title: Core Data Concurrency - Collection was mutated while being enumerated
body: <p>I'm adding some objects called <code>Transaction</code>s in my Core Data application. Those transactions are linked to an account.</p>
  
  <p>I'd like to update the account amount when all the transactions are saved. Sometimes there is a concurrency exception </p>
  
  <blockquote>
    <p>"Collection was mutated while being enumerated"</p>
  </blockquote>
  
  <p>at <code>NSArray *matches = [managedObjectContext executeFetchRequest:request error:&amp;error];</code> line in <code>accountManagedObjectWithId</code> method.</p>
  
  <pre><code>//TransactionsManager
  
  - (BOOL)addRepeatTransaction:(Transaction *)transaction{
  
      Account *accountTrx = transaction.account;
      double accountAmount = accountTrx.amount;
      for (int i =0; i&lt;nbRepeat; i++){
           accountAmount +=transactionBiz.amount;
          [[Persister instance] registerTransaction:transaction];
      }
      [[Persister instance]editAmountAccount:transaction.account amount:accountAmount];
      [[Persister instance]saveContext];
  
      return YES;
  }
  
  //Persister
  
  -(id)init {
      if (self = [super init]){
          managedObjectContext = [appDelegate managedObjectContext];
          return self;
      }
      return nil;
  }
  
  -(BOOL)registerTransaction:(Transaction *)transaction {
      TransactionManagedObject *transactionsRow = (TransactionsManagedObject *)[NSEntityDescription insertNewObjectForEntityForName:@"Transactions" inManagedObjectContext:managedObjectContext];
  
      transactionsRow.idTransaction =  [NSNumber numberWithInt:transaction.idTransaction];
      transactionsRow.name = transaction.name;
      transactionsRow.amount = [NSNumber numberWithDouble:transaction.amount];
      [...]
      transactionsRow.account = [[Finder instance] accountManagedObjectWithId:transaction.account.idAccount];
  
      return YES;
  }
  
  -(BOOL)editAmountAccount:(Account *)asset amount:(double)amount {
      AccountManagedObject *accountRow = [[Finder instance] accountManagedObjectWithId:account.idAccount];
  
      accountRow.amount = [NSNumber numberWithDouble:amount];
      return YES;
  }
  
  -(AccountManagedObject *)accountManagedObjectWithId:(NSInteger)idAccount {
  
      NSFetchRequest *request = [[NSFetchRequest alloc]init];
      [request setEntity:[NSEntityDescription entityForName:@"Accounts" inManagedObjectContext:managedObjectContext]];
  
      //Predicate
      NSString *recordedIdAccount = @"idAccount";
      NSNumber *numberIdAccount = [NSNumber numberWithInt:idAccount];
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", recordedIdAccount,numberIdAccount];
      [request setPredicate:predicate];
  
      //Execute
      NSError *error;
      NSArray *matches = [managedObjectContext executeFetchRequest:request error:&amp;error];
      NSInteger nbResult = [matches count];
      if(nbResult==1){
          return [matches objectAtIndex:0];
      }
      if(nbResult==0){
          [...]
          return nil;
      }
      if(nbResult&gt;0){
          [...]
          return nil;
      }
      return nil;
  }
  
  //UI call
  [...]
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
       //Save
       [[TransactionsManager instance] addRepeatTransaction:transactionToAdd];
  
       dispatch_async(dispatch_get_main_queue(), ^{
  
            //Call the delegate
            [self.delegate theSaveButtonOnTheAddTransactionTVCWasTapped:self];
        });
  });
  </code></pre>
  
  <hr>
  
  <p>@Valentin Radu</p>
  
  <p>I've tried that but the problem remains. I have to set up a <code>managedObjectContext</code> for each adding. This is working but it slows the application. Is it the right way to do it?</p>
  
  <pre><code>-(BOOL)registerTransaction:(Transaction *)transaction{
      NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
     [moc setPersistentStoreCoordinator:[[self managedObjectContext] persistentStoreCoordinator]];
  
  
      TransactionsManagedObject *transactionsRow = (TransactionsManagedObject *)[NSEntityDescription insertNewObjectForEntityForName:@"Transactions" inManagedObjectContext:moc];
  
      transactionsRow.idTransaction =  [NSNumber numberWithInt:transaction.idTransaction];
      transactionsRow.name = transaction.name;
      transactionsRow.amount = [NSNumber numberWithDouble:transaction.amount];
      [...]
      transactionsRow.account = [[Finder instance] accountManagedObjectWithId:transaction.account.idAccount andManagedContext:moc]];
  
      NSError *error = nil;
      if (![moc save:&amp;error]) {
          [[ErrorManager instance] addError:error];
          return NO;
      }
  
      return YES;
  }
  </code></pre>