在背景中添加标记

时间:2013-04-20 21:08:04

标签: google-maps-sdk-ios

我正在尝试在后台添加标记,以防止地图在添加时变得无响应,但我看到了以前从未遇到过的错误。

这是我尝试在后台加载标记,并在尝试发送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

2 个答案:

答案 0 :(得分:1)

根据this documentation

  

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

答案 1 :(得分: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];
      });
    }
});