title: CALayerArray was mutated while being enumerated
body: <p>I have to delete an object of an array every now and then, when I do it I get this error.</p>
  
  <blockquote>
    <p>Collection &lt; CALayerArray: 0xc4f3b20> was mutated while being enumerated</p>
  </blockquote>
  
  <p>The error appears on this method, which is the accesor of the Array:</p>
  
  <pre><code>- (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];
      }
  }
  </code></pre>
  
  <p>As you can see Im taking care of not returning the original array but a copy, but it still crashes.</p>
  
  <p>Also the method where Im deleting elements of the array is this.</p>
  
  <pre><code>- (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);
  
  
  
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks.</p>