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 7813031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:42:55+00:00 2026-06-02T04:42:55+00:00

I have this piece of code which I am using to update some values

  • 0

I have this piece of code which I am using to update some values in Core Data when another object is added:

//Create new receipt
        Receipt *receipt = [[Receipt alloc] init];
        receipt.project = self.projectLabel.text;
        receipt.amount = self.amountTextField.text;
        receipt.descriptionNote = self.descriptionTextField.text;
        receipt.business = self.businessNameTextField.text;
        receipt.date = self.dateLabel.text;
        receipt.category = self.categoryLabel.text;
        receipt.paidBy = self.paidByLabel.text;
        receipt.receiptImage1 = self.receiptImage1;
        //Need to set this to 2
        receipt.receiptImage2 = self.receiptImage1;
        receipt.receiptNumber = @"99";

        int count = 0;
        int catCount = 0;

    for (Project *p in appDelegate.projects)
            {
                if ([p.projectName isEqualToString:receipt.project]){
                    double tempValue = [p.totalValue doubleValue];
                    tempValue += [receipt.amount doubleValue];
                    NSString *newTotalValue = [NSString stringWithFormat:@"%.02f", tempValue];
                    NSString *newProjectName = p.projectName;
                    //remove entity from Core Data
                    NSFetchRequest * allProjects = [[NSFetchRequest alloc] init];
                    [allProjects setEntity:[NSEntityDescription entityForName:@"Project" inManagedObjectContext:appDelegate.managedObjectContext]];
                    [allProjects setIncludesPropertyValues:NO]; //only fetch the managedObjectID

                    NSError * error = nil;
                    NSArray * projectsArray = [appDelegate.managedObjectContext executeFetchRequest:allProjects error:&error];

                    //Delete product from Core Data
                    [appDelegate.managedObjectContext deleteObject:[projectsArray objectAtIndex:count]];

                    NSError *saveError = nil;
                    [appDelegate.managedObjectContext save:&saveError];

                    [appDelegate.projects removeObjectAtIndex:count];

                    NSLog(@"Removed project from Core Data");

                    //Insert a new object of type ProductInfo into Core Data
                    NSManagedObject *projectInfo = [NSEntityDescription
                                                    insertNewObjectForEntityForName:@"Project" 
                                                    inManagedObjectContext:appDelegate.managedObjectContext];

                    //Set receipt entities values
                    [projectInfo setValue:newProjectName forKey:@"name"];
                    [projectInfo setValue:newTotalValue forKey:@"totalValue"];

                    if (![appDelegate.managedObjectContext save:&error]) {
                        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
                    }

                    NSLog(@"Added Project to Core Data");

                    Project *tempProject = [[Project alloc] init];
                    tempProject.projectName = [projectInfo valueForKey:@"name"];
                    tempProject.totalValue = [projectInfo valueForKey:@"totalValue"];

                    [appDelegate.projects addObject:tempProject];

                }
                count++;
            }

            for (Category *c in appDelegate.categories){

                if ([c.categoryName isEqualToString:receipt.category]){

                    double tempValue = [c.totalValue doubleValue];
                    tempValue += [receipt.amount doubleValue];
                    NSString *newTotalValue = [NSString stringWithFormat:@"%.02f", tempValue];
                    NSString *newCategoryName = c.categoryName;

                    //remove entity from Core Data
                    NSFetchRequest * allCategories = [[NSFetchRequest alloc] init];
                    [allCategories setEntity:[NSEntityDescription entityForName:@"Category" inManagedObjectContext:appDelegate.managedObjectContext]];
                    [allCategories setIncludesPropertyValues:NO]; //only fetch the managedObjectID

                    NSError * categoriesError = nil;
                    NSArray * categoriesArray = [appDelegate.managedObjectContext executeFetchRequest:allCategories error:&categoriesError];

                    //Delete product from Core Data
                    [appDelegate.managedObjectContext deleteObject:[categoriesArray objectAtIndex:catCount]];
                    NSError *categorySaveError = nil;
                    [appDelegate.managedObjectContext save:&categorySaveError];

                    [appDelegate.categories removeObjectAtIndex:catCount];

                    NSLog(@"Removed category from Core Data");
                    NSError * error = nil;
                    //Insert a new object of type ProductInfo into Core Data
                    NSManagedObject *categoryInfo = [NSEntityDescription
                                                     insertNewObjectForEntityForName:@"Category" 
                                                     inManagedObjectContext:appDelegate.managedObjectContext];

                    //Set receipt entities values
                    [categoryInfo setValue:newCategoryName forKey:@"name"];
                    [categoryInfo setValue:newTotalValue forKey:@"totalValue"];

                    if (![appDelegate.managedObjectContext save:&error]) {
                        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
                    }

                    NSLog(@"Added Category to Core Data");

                    Category *tempCategory = [[Category alloc] init];
                    tempCategory.categoryName = [categoryInfo valueForKey:@"name"];
                    tempCategory.totalValue = [categoryInfo valueForKey:@"totalValue"];

                    [appDelegate.categories addObject:tempCategory];
                }
                catCount++;

            }

This code gives the error:

‘…was mutated while being enumerated’.

Can anyone explain why? Also, is there a better approach to doing what I’m trying to achieve?

  • 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-02T04:42:57+00:00Added an answer on June 2, 2026 at 4:42 am

    The error you’re seeing is accurate. The problem is that you’re mutating (changing) a collection while iterating over it. Basically, you’re doing something of the form:

    for (Project *p in appDelegate.projects) {
       ...
       [p addObject: X]
    }
    

    This isn’t allowed.

    One simple solution is to make a new collection of the objects you want to add, and then add them to the original container outside of your loop. Something like:

    NSMutableArray *array = [NSMutableArray array];
    for (Project *p in appDelegate.projects) {
       ...
       [array addObject:X];
    }
    [p addObjects:array];
    

    By the way, did you google for the error text “was mutated while being enumerated”? I’d be surprised if you didn’t find the answer for this common problem just by googling.

    Also, when posting an error message, it’s helpful to post the full line, not just part of it.

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

Sidebar

Related Questions

I have this piece of code which i am using on another area of
I have this piece of code which I'm hoping will be able to tell
I am somewhat new to R, and i have this piece of code which
Ok i have this piece of code from which i took from W3schools :-
I have this piece of Open MP code here which performs an integeration of
I have this piece of code: if(($op == q)); then which throws out this
We have this piece of code: <a class=loadbutton onClick=$('checkout_form').submit(); $(this).update('<img src=\'images/loader/longLoad.gif\' /> Processing'); return
I have this piece of code (summarized)... AnsiString working(AnsiString format,...) { va_list argptr; AnsiString
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
I have this piece of code: $(#faq).click(function () { var url = $.get(faq, {

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.