title: Core data - Collection NSCFSet was mutated while being enumerated
body: <p>I'm stuck on a problem while inserting data. I am checking the condition if data not exist then insert but app crashes on    </p>
  
  <pre><code>BOOL isExist = [context countForFetchRequest:request error:&amp;error];
  </code></pre>
  
  <p>with error Collection <code>__NSCFSet</code> was mutated while being enumerated.</p>
  
  <p>but there are simultaneously more operations are running on background threads. which is going to do more operation with core data.</p>
  
  <pre><code>appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
          NSManagedObjectContext *context = appDelegate.managedObjectContext;
  
      NSError *error;
      for (StreamData *stream in streamData) {
          NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:DB_TABLE_STREAM];
          [request setPredicate:[NSPredicate predicateWithFormat:@"stream_id = %@", stream.messageId]];
          [request setFetchLimit:1];
          BOOL isExist = [context countForFetchRequest:request error:&amp;error];
  
          if (!isExist) {
              CF_Stream *object = [NSEntityDescription insertNewObjectForEntityForName:DB_TABLE_STREAM inManagedObjectContext:context];
              [object setStream_id:[NSNumber numberWithLongLong:[stream.messageId longLongValue]]];
              [object setType:stream.streamType];
              [object setTime_stamp:[[DateTimeManager sharedInstances] getStreamDate:stream.streamTime]];
          }
      }
      if (![context save:&amp;error]) {
          NSLog(@"Failed to sve - error: %@", error);
          return NO;
      } else {
          return YES;
      }
  </code></pre>
  
  <p>Any help is much appreciated.
  Thanks!</p>