CALayerArray was mutated while being enumerated

匿名 (未验证) 提交于 2019-12-03 08:30:34

问题:

I have to delete an object of an array every now and then, when I do it I get this error.

Collection < CALayerArray: 0xc4f3b20> was mutated while being enumerated

The error appears on this method, which is the accesor of the Array:

- (NSArray *)occupantsArray {     if (dispatch_get_current_queue() == moduleQueue)     {         return occupantsArray;     }     else     {         __block NSArray *result;          dispatch_sync(moduleQueue, ^{ //ON THIS LINE             result = [occupantsArray copy];         });          return [result autorelease];     } } 

As you can see Im taking care of not returning the original array but a copy, but it still crashes.

Also the method where Im deleting elements of the array is this.

- (void)eraseJIDFromArray:(NSString*)jid{      dispatch_block_t block = ^{         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];         int i = 0;         for(NSString *jidAct in occupantsArray){             if([jidAct isEqualToString:jid]){                 [occupantsArray removeObjectAtIndex:i];             }             i++;         }         [pool drain];     };      if (dispatch_get_current_queue() == moduleQueue)         block();     else         dispatch_async(moduleQueue, block);    } 

The array can have upto 200 elements, so it can take some time to go through all of them, but I'm setting queues, dont know what else I can do.

Any ideas?

Thanks.

回答1:

This code:

for(NSString *jidAct in occupantsArray){     if([jidAct isEqualToString:jid]){         [occupantsArray removeObjectAtIndex:i];     }     i++; } 

is probably causing your problems. You shouldn't be removing elements while enumerating the array. The way you avoid this is by using an NSMutableIndexSet:

NSMutableIndexSet *indexes = [NSMutableIndexSet set]; // assuming NSInteger i; for(NSString *jidAct in occupantsArray){     if([jidAct isEqualToString:jid]){         [indexes addIndex:i];     }     i++; } [occupantsArray removeObjectsAtIndexes:indexes]; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!