2013-04-20 26 views
0

我試圖在背景中添加標記以防止地圖在添加時變得無響應,但是我看到一個我從未遇到過的錯誤。在背景中添加標記

這是我嘗試在後臺加載標記,並嘗試發送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 

回答

1

根據this documentation

GMSMapView只能讀取,並從主線程,類似 所有的UIKit對象修改。從另一個線程調用這些方法將會導致異常或未定義的行爲 。

0
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