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
4

2 回答 2

1

根据此文档

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

于 2013-04-21T12:12:30.463 回答
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];
      });
    }
});
于 2013-04-21T22:11:18.233 回答