在背景中添加标记

问题描述:

我试图在背景中添加标记以防止地图在添加时变得无响应,但是我看到一个我从未遇到过的错误。在背景中添加标记

这是我尝试在后台加载标记,并尝试发送addMarkerWithOptions时发生错误。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    NSLog(@"yeap im in the background %i, markers=%i", [[self visibleLocations] count], [[[self googleMap] markers] count]); 
    for (int i = 0; i < self.visibleLocations.count; i++) { 
     Location *location = [self.visibleLocations objectAtIndex:i]; 
     GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init]; 
     options.position = CLLocationCoordinate2DMake([location.lat floatValue], [location.lng floatValue]); 
     options.title = [NSString stringWithFormat:@"(%i) %@", location.mapNeighbors.count, location.title]; 
     options.icon = [UIImage imageNamed:@"search_measle_small.png"]; 
     [self.googleMap addMarkerWithOptions:option]; 
    } 
}); 

错误

yeap im in the background 860, markers=0 
2013-04-20 14:02:11.561 Skate-Spots[7796:907] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSSetM: 0x1d6e7880> was mutated while being enumerated.' 
*** First throw call stack: 
(0x336a23e7 0x3b39d963 0x336a1ec1 0x16ea0d 0x3b7b7793 0x3b7b75db 0x3b7bae45 0x336761b1 0x335e923d 0x335e90c9 0x371c833b 0x355052b9 0x112e5 0x3b7cab20) 
libc++abi.dylib: terminate called throwing an exception 

根据this documentation

GMSMapView只能读取,并从主线程,类似 所有的UIKit对象修改。从另一个线程调用这些方法将会导致异常或未定义的行为 。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    NSLog(@"yeap im in the background %i, markers=%i", [[self visibleLocations] count], [[[self googleMap] markers] count]); 
    for (int i = 0; i < self.visibleLocations.count; i++) { 
     Location *location = [self.visibleLocations objectAtIndex:i]; 
     GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init]; 
     options.position = CLLocationCoordinate2DMake([location.lat floatValue], [location.lng floatValue]); 
     options.title = [NSString stringWithFormat:@"(%i) %@", location.mapNeighbors.count, location.title]; 
     options.icon = [UIImage imageNamed:@"search_measle_small.png"]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.googleMap addMarkerWithOptions:option]; 
     }); 
    } 
}); 
+1

您应该详细说明一下并解释错误是什么 – blue 2013-04-21 22:28:59