MapKit animations on tvOS result in hang

Hardware: Apple TV 4K (1st gen) OS: tvOS 17.1

I have been attempting to create a simple dashboard using MapKit for my company's devices located around the globe. As of now, we only have 7-8 attached to the main database, so I'm using an API client I created to load that many Map markers after the View is loaded.

My issue is that despite this being a relatively low-intensity task, the Map view crashes after about 15 minutes, with no errors or any indications of memory leaks or other problems that would indicate something is wrong. GPU usage spikes on initial load, but remains in the green throughout the process. I tried the same code in swift playgrounds and was able to get the globe to spin indefinitely, probably because it's using the extra resources available on my M2 Max chip. But again, I am well below the memory and CPU usage limits available for tvOS. other than initially loading 7-8 objects asynchornously on initial load, no other work is being done.

MRE:

import SwiftUI
import MapKit

struct ContentView: View {
    
    let timer = Timer.publish(every: 0.1, on: .main, in: .default).autoconnect()
    
    @State var viewport: MapCameraPosition = .region(MKCoordinateRegion(center: .init(latitude: 0, longitude: 0), span: .init(latitudeDelta: 90, longitudeDelta: 180)))
    
    @State var longitude: Double = 0 {
        didSet {
            viewport = .region(MKCoordinateRegion(center: .init(latitude: 20, longitude: longitude), span: .init(latitudeDelta: 90, longitudeDelta: 180)))
        }
    }
    
    private func shiftLongitude(by: Double) {
        if self.longitude >= 180 {
            self.longitude -= 360
        } else {
            self.longitude += by
        }
    }
    
    var body: some View {
        Map(position: $viewport) {
            
        }
        .mapStyle(.imagery(elevation: .realistic))
        .onReceive(timer) { _ in
            withAnimation {
                self.shiftLongitude(by: 1.0)
            }
        }
    }
}
  • I believe since tvOS 17.0 there's a new View method called .mapCameraKeyframeAnimator() but this method is has little to no documentation and doesn't support looping.

Add a Comment