I am adding around 1000 MKPolygon
s onto an MKMapView
.
After adding the overlays onto the MKMapView
it works fine. However if I zoom in and zoom out quickly (occasionally letting go of the zoom so the mapView
processes it's new visibleMapRect
) I find the app (sometimes) crashes with the following exception stack trace:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSSetM removeObject:]: object cannot be nil'
*** First throw call stack:
(
0 CoreFoundation 0x0000000107db6b0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010781b141 objc_exception_throw + 48
2 CoreFoundation 0x0000000107d1712a -[__NSSetM removeObject:] + 538
3 VectorKit 0x000000010bae84fa -[VKRasterOverlayTileSource removeOverlay:] + 89
4 VectorKit 0x000000010b79a648 -[VKMapModel removeRasterOverlay:] + 68
5 MapKit 0x0000000106f93bf1 -[MKOverlayContainerView _removeDrawable:forOverlay:level:] + 502
6 MapKit 0x0000000106f92633 -[MKOverlayContainerView addAndRemoveOverlayViews] + 785
7 MapKit 0x0000000106f44772 -[MKMapView _didChangeRegionMidstream:] + 229
8 MapKit 0x0000000106f49528 -[MKMapView mapLayer:didChangeRegionAnimated:] + 91
9 VectorKit 0x000000010b7d39e8 -[VKMapCameraController rotateToYaw:withPoint:animated:] + 884
10 VectorKit 0x000000010b7d4e7b -[VKMapCameraController snapMapIfNecessary:] + 389
11 MapKit 0x0000000106f93bf1 -[MKOverlayContainerView _removeDrawable:forOverlay:level:] + 502
12 MapKit 0x0000000106f92633 -[MKOverlayContainerView addAndRemoveOverlayViews] + 785
13 MapKit 0x0000000106f44772 -[MKMapView _didChangeRegionMidstream:] + 229
14 MapKit 0x0000000106f49528 -[MKMapView mapLayer:didChangeRegionAnimated:] + 91
15 VectorKit 0x000000010b9f7b0e -[VKScreenCameraController stopPinchingWithFocusPoint:] + 64
16 MapKit 0x0000000106fbb1e3 __38-[MKMapGestureController handlePinch:]_block_invoke.184 + 126
17 VectorKit 0x000000010b78aaa2 -[VKAnimation stopAnimation:] + 109
18 VectorKit 0x000000010b8f9a3d -[VKDynamicAnimation stopAnimation:] + 45
19 MapKit 0x0000000106fb7819 -[MKMapGestureController stopDynamicAnimations] + 50
20 MapKit 0x0000000106fba254 -[MKMapGestureController gestureRecognizerTouchesBegan:] + 39
21 MapKit 0x0000000106fbbab1 -[_MKUserInteractionGestureRecognizer touchesBegan:withEvent:] + 198
22 UIKit 0x0000000108e9b934 -[UIGestureRecognizer _touchesBegan:withEvent:] + 113
23 UIKit 0x0000000108e8901c __55-[UIGestureEnvironment _updateGesturesForEvent:window:]_block_invoke + 337
24 UIKit 0x0000000108e89b79 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 282
25 UIKit 0x0000000108e88e0a -[UIGestureEnvironment _updateGesturesForEvent:window:] + 274
26 UIKit 0x00000001089d4eea -[UIWindow sendEvent:] + 4092
27 UIKit 0x0000000108981a84 -[UIApplication sendEvent:] + 352
28 UIKit 0x00000001091655d4 __dispatchPreprocessedEventFromEventQueue + 2926
29 UIKit 0x000000010915d532 __handleEventQueue + 1122
30 UIKit 0x000000010915e800 __handleEventQueue + 5936
31 CoreFoundation 0x0000000107d5cc01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
32 CoreFoundation 0x0000000107d420cf __CFRunLoopDoSources0 + 527
33 CoreFoundation 0x0000000107d415ff __CFRunLoopRun + 911
34 CoreFoundation 0x0000000107d41016 CFRunLoopRunSpecific + 406
35 GraphicsServices 0x000000010cc97a24 GSEventRunModal + 62
36 UIKit 0x0000000108964134 UIApplicationMain + 159
37 MyProject 0x0000000106e51a9f main + 111
38 libdyld.dylib 0x000000010e56165d start + 1
This doesn't always happen. Often it's quite rare. You can see that it's hard to debug and work out what's going wrong because my project doesn't appear in the stack trace (except that it's running in my application...).
When trying to track down the issue I created a very basic app with nothing but an MKMapView
and 1000 random MKPolygons
with 5 randomly generated CLLocationCoordinate2D
each.
I found that the app still crashes when zooming, resting, zooming, resting, etc. But less likely?
All I am doing here is adding basic random MKPolygon
s to the MKMapView
!
Note the MKPolygonRenderer
for each MKPolygon
does nothing apart from give it a random fillColor
.
Note: I am running the application in Xcode on an iPhone 7 Simulator.
What's going wrong here? Thanks!
I had a very similar problem when adding & removing annotations to a map view.
The answer turned out to be put everything that accesses the annotations - even calculations that only read the annotations array - onto the main thread.
I had only put the actual adding & removing on the main thread and it was ok for the most part, but would randomly crash with