views:

306

answers:

2

I have a MKMapView and another class has a thread adding annotations to the MKMapView.

But the problem is, MKMapView.annotations is nonatomic. So while the other class is adding annotations.... It looks OK if user do not move the map's visible area. But if you keep moving it, and the other class was adding annotations to the map at that moment, app crashes. Because the array was mutated during map was animating.

I made NSOperationQueue, and add each adding annotation job to the queue.

And then whenever map's region is changed, pause the NSOperationQueue.. and then restart it when region change is done.

I could get rid of most crashes.. but still it is happened occasionally.

Any idea?

+2  A: 

Don't add the annotations to the MKMapView in a background thread. Let the background operation return an NSArray back to the main thread, then add the NSArray to the MKMapView synchronously. I do this all the time and it works.

bentford
Thanks a lot! I will do that way.
A: 

Always handle UI controls on the main thread.

Hussain Saleem