2

我在 iOS 6 中加载地图视图时遇到问题。但它在以前的操作系统版本中 100% 工作。

这是代码:

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

这是我的 .m 文件中的代码结构。我已经合成了变量并在dealloc方法中发布了它们。

//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;

}

我得到的例外是:

*** 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)

但如果我使用:

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

该应用程序工作正常。

知道为什么会这样吗?

4

1 回答 1

0

发生这种情况的原因有很多 - 错误告诉您,您(或底层 iOS 库)在某个时候试图枚举一个在该过程进行时发生更改的数组。

当你populateMap关闭主线程时它工作并且当你在辅助线程上调用它时它不起作用的事实表明存在某种竞争条件。

请记住,并非所有UIKit都是线程安全的(大部分都不是) - 所以那里可能存在问题。您将注释添加到地图中,同时仍在后台线程中:

[galleryListMap addAnnotation:addrAnnotation];

...这也是您的崩溃发生的路线。理所当然地,当您向 MapView 添加注释时,它可能会遍历其所有当前注释以更新显示。因为您是在后台线程上进行这些调用,所以这可能会带来很多问题。

相反,试试这个:

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

这将强制地图视图在主线程上添加注释。作为一般规则,只有少数东西UIKit是线程安全的(苹果文档有一个完整的列表)。

于 2012-10-12T15:02:07.697 回答