Search code examples
objective-cnsarraydispatch

CALayerArray was mutated while being enumerated


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.


Solution

  • 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];