Insert annotation view fails in iOS 11.0.3

Viewed 998

I released an app to App Store some days ago, and today I went to check if there were any crashes. I found out that on two different devices, I had the same crash, related to MKSpatialCollider's insertAnnotationView. Both devices mount iOS 11.0.3.
I use MapKit in a ViewController to display custom AnnotationView objects, that I also group in clusters, that's all. I have no idea about these crashes, what could be happening in my app that triggers these events?
Below, I attached those two exceptions' stacktraces, but if you need something else to figure out what's happening, just let me know:

First exception's stacktrace

enter image description here

Second exception's stacktrace

enter image description here

Update 1 - Here's the link to the code that should trigger the errors (I'm not sure since Xcode points to another file that doesn't contain MapKit at all): https://gist.github.com/MrOverflOOw/bbc14d546ef10bd17065af08fdf3e27c

5 Answers

I ran into this error when attempting to remove an instance of MKClusterAnnotation from the map views annotations. Specifically when a annotation view is reused for an MKClusterAnnotation. Check to see if you do anything like:

[mapView removeAnnotations:mapView.annotations];

or if you're in Swift:

mapView.removeAnnotations(mapView.annotations)

or really any line of code where you might muck with an MKClusterAnnotation provided by MapKit.

The workaround that worked for me was to deselect annotations (cluster ones are the problem) before adding or removing any other annotations.

I realize your case may not involve adding or removing annotations, but my app crashes in the same MKSpatialCollider code path, so thought I'd mention this workaround. You might try deselecting annotations before the view goes away.

override func viewWillDisappear(_ animated: Bool) {  
    super.viewWillDisappear(animated)  
    mapView.removeAnnotations(mapView.annotations)  
}  

Seems to fix the crash for me

Related