Core Data was mutated while being enumerated
<p>I've an annoying problem with Core Data. My app need to get contacts from iPhone and save it in my database. I'm trying to do that in background thread.</p>

<p><strong>I use above code for that:</strong></p>

<pre><code>[self performSelectorInBackground:@selector(fetchingContact) withObject:nil];


    -(void)fetchingContact{
    // Some Code
    for (int i = 0; i &lt; nPeople; i++)
    {
    //Some Code
        NSManagedObjectContext *context = [APP_DELEGATE managedObjectContext];
        ABRecordID recordID = ABRecordGetRecordID(person);
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:context];
        [fetchRequest setEntity:entity];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(contactId = '%d')",recordID]];
        [fetchRequest setPredicate:predicate];
        NSError *error = nil;
//crash
        NSArray *contactObjArray = [context executeFetchRequest:fetchRequest error:&amp;error];
//crash
        if (error) {}
        Contact *contacts;
        if (contactObjArray.count == 0) {
            contacts = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:context];
        }else {
            contacts = [contactObjArray objectAtIndex:0];
        }
    //Some Code
    }
    }
</code></pre>

<p><strong>In AppDelegate:</strong></p>

<pre><code>- (NSManagedObjectContext *)managedObjectContext
{
     NSLog(@"managedObjectContext");
    // Returns ;the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
     if (_managedObjectContext != nil) {
          return _managedObjectContext;
     }

     NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
     if (!coordinator) {
          return nil;
     }
     _managedObjectContext =  [[NSManagedObjectContext alloc]initWithConcurrencyType: NSMainQueueConcurrencyType];
     [_managedObjectContext setPersistentStoreCoordinator:coordinator];
     return _managedObjectContext;
}
</code></pre>

<p>Here I try to save in my <code>Core Data</code> but it crash with error :</p>

<blockquote>
  <p><strong>* Terminating app due to uncaught exception 'NSGenericException', reason: '*</strong> Collection &lt;__NSCFSet: 0x15fad880> was mutated while being enumerated.'` at this line:</p>
</blockquote>

<pre><code> NSArray *contactObjArray = [context executeFetchRequest:fetchRequest error:&amp;error];
</code></pre>

<p>I search already online, I found a lot of things but nothing helps me. When I run that, there is no place where Core Data is changed, or Contact entity. That does this error very strange.</p>

<p>If I run it in main thread I get no errors/ no crash, but if the app is quit in that time (while is executed) I lose all content from <code>Contact</code></p>

<p>Please, any help. Tell me if I need to provide more information.</p>