views:

655

answers:

1

Hi guys! I'm having some problem with MKMapView / annotations / UINavigationController. Basically, i manage my views using a UINavigationController; one of my view contains an MKMapView and i add annotations on it (10 to 200) using the addAnnotations method.

Everything is working fine except for one thing : if i navigate "too fast" on my UINavigationController, the iphone simulator crashes, receiving an "EXC BAD ACCESS" signal. For example, if i load my view containing the MKMapView and immediatly press the "Back" button from the UINavigationController navigation bar, i get the signal. I figured that the problem was with the addAnnotations method : when my MKMapView is loaded, i add annotations to it but it looks like everything is done asynchronously. If i wait like a second before pushing the "Back" button, i get no error but if i'm too fast, it crashes. I get no error at all if i remove the addAnnotations line. I guess it's because my view is released by the UINavigationController BEFORE the addAnnotations method got the job done.

Any good solution to this? I don't want the user to wait (displaying a loading view for example); i guess the solution might be a better memory management but i don't see how i could do this.

    if(DEBUG_MODE) { NSLog(@"Creating array of placemarks : begin"); }
self.placemarkCache = [[NSMutableArray alloc] init];
// Loading placemarks for a placemark dictionary
NSArray *sortedKeys = [[self.placemarkDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
for (id key in sortedKeys) {
 MyPlacemark *currentPlacemark = [self.placemarkDictionary objectForKey:key];
 [self.placemarkCache addObject:currentPlacemark];
 [currentPlacemark release];
}
if(DEBUG_MODE) { NSLog(@"Creating array of placemarks : done"); }
if(DEBUG_MODE) { NSLog(@"Adding placemarks : begin"); }
[self.mapView addAnnotations:self.placemarkCache];
if(DEBUG_MODE) { NSLog(@"Adding placemarks : done"); }

On this example, i get the "Adding placemarks : done" message before anything get displayed on the map.

+1  A: 

I ran into the same issue or bug, and found a solution on a blog post.

You can see if what you're seeing is the same as the issue in the post by looking at the debugger stack trace.

The issue is due to the fact the MapViewController (the parent of the MKMapView) has been dealloc'd and the async map fetching internals of MKMapView is trying to call the delegate of MKMapView (which was MapViewController).

So, on dealloc of MapViewController, you've got to reset the MKMapView.delegate = nil so that no delegates are called after some tiles get returned.

CVertex
That's exactly the same issue and it's working :)It sounds obvious now...Thanks for your answer!
Vivi