Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8966099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:57:11+00:00 2026-06-15T16:57:11+00:00

I’m adding some objects called Transaction s in my Core Data application. Those transactions

  • 0

I’m adding some objects called Transactions in my Core Data application. Those transactions are linked to an account.

I’d like to update the account amount when all the transactions are saved. Sometimes there is a concurrency exception

“Collection was mutated while being enumerated”

at NSArray *matches = [managedObjectContext executeFetchRequest:request error:&error]; line in accountManagedObjectWithId method.

//TransactionsManager

- (BOOL)addRepeatTransaction:(Transaction *)transaction{

    Account *accountTrx = transaction.account;
    double accountAmount = accountTrx.amount;
    for (int i =0; i<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:&error];
    NSInteger nbResult = [matches count];
    if(nbResult==1){
        return [matches objectAtIndex:0];
    }
    if(nbResult==0){
        [...]
        return nil;
    }
    if(nbResult>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];
      });
});

@Valentin Radu

I’ve tried that but the problem remains. I have to set up a managedObjectContext for each adding. This is working but it slows the application. Is it the right way to do it?

-(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:&error]) {
        [[ErrorManager instance] addError:error];
        return NO;
    }

    return YES;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:57:12+00:00Added an answer on June 15, 2026 at 4:57 pm

    You seem to be using only one context, which is wrong if you have parallel operations executing on your core data objects. You should have a context per thread and merge the contexts every time you save. Apple’s Core Data Guideline talks about this in more detail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.