Mapview crashes in iOS 6

2019-07-14 08:41发布

I'm having a problem when I'm loading map view in iOS 6. But it's working 100% with previous OS versions.

Here's the code:

//.h file
MKMapView *galleryListMap; //map view
NSMutableArray *copyOfAllRecords; //array with locations

And here is the code structure in my .m file. I have synthesized variables and released them in dealloc method.

//this will runs in a background thread and populateMap method will make all the   annotations
//[self performSelectorInBackground:@selector(populateMap) withObject:nil];

//here is populateMap method
-(void)populateMap{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 
NSArray *existingpoints = galleryListMap.annotations;

//removes alll the existing points
[galleryListMap removeAnnotations:existingpoints];



//assign point to map view
for(int i = 0; i < [copyOfAllRecords count]; i++) {

     Gallery  *gallery = (Gallery *)[copyOfAllRecords objectAtIndex:i];//array which has the elements

     //create location based on the latitude and longtitude
     CGFloat latDelta = gallery.latitude;
     CGFloat longDelta = gallery.longitude;
     CLLocationCoordinate2D newCoord = {latDelta, longDelta};

      //adds the notations
     AddressAnnotation *addrAnnotation = [[[AddressAnnotation alloc] initWithCoordinate:newCoord]autorelease];
     //assing the location the map

    [addrAnnotation setId:i];
    [addrAnnotation setTitle:gallery.name];

    if([gallery.exhibition length]==0 && !gallery.isResturant){
        [addrAnnotation setSubtitle:@""];

    }else{
        [addrAnnotation setSubtitle:gallery.exhibition];
    }

    **//gives the exception on this line
    [galleryListMap addAnnotation:addrAnnotation];**

}

MKCoordinateRegion region;
MKCoordinateSpan span = MKCoordinateSpanMake(0.2, 0.2);
Gallery *lastGalleryItem = [copyOfAllRecords lastObject];
CLLocationCoordinate2D location = {lastGalleryItem.latitude, lastGalleryItem.longitude};

region.span=span;
region.center=location;

[galleryListMap setRegion:region animated:TRUE];
[galleryListMap regionThatFits:region];

galleryListMap.showsUserLocation = YES;

[pool release];

}

- (MKAnnotationView *) mapView:(MKMapView *)mkmapView viewForAnnotation:(AddressAnnotation *) annotation{


   static NSString *identifier = @"currentloc";

if([annotation isKindOfClass:[AddressAnnotation class]]){

    MKPinAnnotationView *annView = [[[MKPinAnnotationView alloc]initWithAnnotation:addAnnotation reuseIdentifier:identifier]autorelease];

    Gallery *galItem = (Gallery *)[copyOfAllRecords objectAtIndex:annotation.annotationId];

    CGRect viewFrame;
    UIView *myView;
    UIImage *tagImage;

     //checks whether the resturant or not
    if (galItem.isResturant) {

     viewFrame = CGRectMake( 0, 0, 41, 45 );
     myView = [[UIView alloc] initWithFrame:viewFrame];
     tagImage= [UIImage imageNamed:@"map_restaurant"];

    }else{

    viewFrame = CGRectMake( 0, 0, 41, 45 );
    myView = [[UIView alloc] initWithFrame:viewFrame];
    tagImage= [UIImage imageNamed:galItem.mapMarker];

    }


    UIImageView *tagImageView = [[UIImageView alloc] initWithImage:tagImage];
    [myView addSubview:tagImageView];
    [tagImageView release];


    UIGraphicsBeginImageContext(myView.bounds.size);
    [myView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();



    [annView setImage:viewImage];
    [myView release];

    int recordCount =[copyOfAllRecords count];


    if (recordCount != 0 ) {
        UIButton *annotationButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [annotationButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];
        annotationButton.tag  = [annotation annotationId];
        annView.rightCalloutAccessoryView = annotationButton;
    }


    if([annotation annotationId]==recordCount-1){
        //hides loading screen
         //[HUD hide:YES];
         self.mapAlreadyLoaded = YES;

    }


    return annView;


}


return nil;

}

The exception I get is:

*** Terminating app due to uncaught exception 'NSGenericException', 
reason: '*** Collection <__NSArrayM: 0x21683870> was mutated while being enumerated.'
*** First throw call stack:
(0x32dc62a3 0x3259897f 0x32dc5d85 0x37c4d33b 0x37c50373 0x86e63 0x37dcc67d 0x36e52311 0x36e521d8)

But if I use:

[self populateMap];
//[self performSelectorInBackground:@selector(populateMap) withObject:nil];

the application works fine.

Any idea why this is happening?

1条回答
唯我独甜
2楼-- · 2019-07-14 09:07

There are a number of reasons this could be happening - the error is telling you that at some point you (or an underlying iOS library) tried to enumerate an array that changed while that process was on-going.

The fact that it works when you populateMap off the main thread and doesn't work when you call it on a secondary thread suggests there's some sort of race condition going on.

Bear in mind that not all of UIKit is thread safe (most of it isn't) - so there might be an issue there. You add the annotations to your map whilst still on a background thread:

[galleryListMap addAnnotation:addrAnnotation];

...this is also the line on which your crash happens. It stands to reason that when you add an annotation to a MapView it probably iterates through all its current annotations to update the display. Because you're making these calls off a background thread this could introduce lots of problems.

Instead, try this:

[galleryListMap performSelectorOnMainThread:@selector(addAnnotation:)
                                 withObject:addrAnnotation 
                               waitUntilDone:YES]

This will force the map view to add the annotation on the main thread. As a general rule, there are only a few things in UIKit that are thread-safe (the apple documentation has a complete list).

查看更多
登录 后发表回答