NSGenericException',原因:'*** Collection< __ NSArrayM:0x12a9f7d0>在MAPS中进行枚举时发生了变异

时间:2013-09-30 14:00:28

标签: iphone objective-c arrays nsmutablearray fast-enumeration

我的堆栈跟踪与地图相关..

NSGenericException发生,如果我们尝试修改正在枚举的数组...我已经注意不要修改枚举数组。

for (int k=0;k<[[af factsArray] count];k++)
        //here af is my object
    {
    Facts *f = [[af factsArray] objectAtIndex:k];

    //here i'm removing unecessary characters from my point String

    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"POINT()"];

    NSString *pointsStr = [[[f.factPosition mutableCopy] componentsSeparatedByCharactersInSet:doNotWant] componentsJoinedByString:@""];

    NSArray *pieces = [[pointsStr componentsSeparatedByString:@" "] copy];
    CGPoint point = CGPointMake([[pieces objectAtIndex:2] floatValue], [[pieces objectAtIndex:1] floatValue]);
    NSValue *val = [NSValue valueWithCGPoint:point];

    [routePointsArray addObject:val];

}

NSInteger pointsCount = routePointsArray.count;

CLLocationCoordinate2D pointsToUse[pointsCount];

for(int i = 0; i < pointsCount; i++)
{
    CGPoint p = [[routePointsArray objectAtIndex:i] CGPointValue];
    pointsToUse[i] = CLLocationCoordinate2DMake(p.x,p.y);
}

MKPolyline *myPolyline = [MKPolyline polylineWithCoordinates:pointsToUse     count:pointsCount];
[routeOverlays addObject:myPolyline];

NSLog(@"routeOverlays count:%d",[routeOverlays count]);


//adding overlays

dispatch_async(dispatch_get_main_queue(), ^{
    [self.map addOverlays:routeOverlays];
});

任何人都可以告诉我代码的问题,因为它有时会崩溃,有时却不会崩溃。 这是我的stacktrace

 *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x12a9f7d0> was mutated while being enumerated.'
        *** First throw call stack:
        (
            0   CoreFoundation                      0x01a2f5e4 __exceptionPreprocess + 180
            1   libobjc.A.dylib                     0x017b28b6 objc_exception_throw + 44
            2   CoreFoundation                      0x01abf3b5 __NSFastEnumerationMutationHandler + 165
            3   VectorKit                           0x04029d7e -[VKMapModel layoutScene:withContext:] + 3854
            4   VectorKit                           0x0401fc8e -[VKModelObject layoutSceneIfNeeded:withContext:] + 110
            5   VectorKit                           0x04028bff -[VKMapModel layoutSceneIfNeeded:withContext:] + 143
            6   VectorKit                           0x0401fd35 -[VKModelObject layoutSceneIfNeeded:withContext:] + 277
            7   VectorKit                           0x0403f278 -[VKWorld layoutScene:withContext:] + 56
            8   VectorKit                           0x0402eac6 -[VKScreenCanvas drawWithTimestamp:] + 358
            9   VectorKit                           0x04019c15 -[VKMapCanvas drawWithTimestamp:] + 149
            10  VectorKit                           0x0402e4ee -[VKScreenCanvas onTimerFired:] + 142
            11  libobjc.A.dylib                     0x017c481f -[NSObject performSelector:withObject:] + 70
            12  VectorKit                           0x041adbdc -[VGLDisplayLink _displayLinkFired:] + 60
            13  QuartzCore                          0x045f3fca _ZN2CA7Display15DisplayLinkItem8dispatchEv + 48
            14  QuartzCore                          0x045f3e86 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 310
            15  QuartzCore                          0x045f43ab _ZN2CA7Display16TimerDisplayLink8callbackEP16__CFRunLoopTimerPv + 123
            16  CoreFoundation                      0x019edc46 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
            17  CoreFoundation                      0x019ed62d __CFRunLoopDoTimer + 1181
            18  CoreFoundation                      0x019d5698 __CFRunLoopRun + 1816
            19  CoreFoundation                      0x019d4b33 CFRunLoopRunSpecific + 467
            20  CoreFoundation                      0x019d494b CFRunLoopRunInMode + 123
            21  GraphicsServices                    0x02c469d7 GSEventRunModal + 192
            22  GraphicsServices                    0x02c467fe GSEventRun + 104
            23  UIKit                               0x0031894b UIApplicationMain + 1225
            24  SnapTraq                            0x0002029d main + 141
            25  libdyld.dylib                       0x022c1725 start + 0
        )
        libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:1)

我要感谢@micantox试图解决我的问题......

无论如何,我已经解决了自己的问题。

我试图从不同的线程执行循环..导致实际问题。

一个线程使用NSArray添加注释,而其他线程正在删除那些NSArray个对象。为了避免这个问题我用过

@synchronized(self)
  {
       // your code goes here 
  }