I'm working with a SwiftUI view that contains a Map with numerous Annotations (ranging from 30 to 100). Alongside these, there's a specific annotation that changes based on the user's selection from a list. The list selection might change rapidly, possibly up to 10 times a second.
When the selected annotation is updated, it seems that all the annotations on the map view get reloaded. This results in high CPU usage and a significant drop in screen refresh rate, especially during rapid selection changes.
How can I optimize this to only reload the selected annotation in the map view instead of all annotations when the selectedAnnotation is changed?
Here's the relevant pseudo code:
@State var annotations: [MKAnnotation]()
@State var selectedAnnotation: MKAnnotation?
Map {
ForEach(annotations.indices, id: \.self) { idx in
let anno = annotations[idx]
Annotation("", coordinate: anno.coordinate) {
Circle()
.stroke(.black, lineWidth: 4)
.fill(.white)
.frame(width: 8, height: 8)
}
}
if let selectedAnnotation {
Annotation("", coordinate: selectedAnnotation.coordinate) {
Circle()
.stroke(.orange, lineWidth: 4)
.fill(.white)
.frame(width: 16, height: 16)
}
} else {
EmptyMapContent()
}
}