views:

88

answers:

2

Im a creating an app for my iPhone using coredata.

I have a viewcontroller with an object i want to save that object to my FavoriteViewController. By clicking a button favorite I want my object to be save into the managedObjectContext but I'm getting the following error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to perform another operation with a fetch already in progress'. *

My Code:

// Step 1: Create Object
Favorite * newFavorite = (Favorite*)[NSEntityDescription insertNewObjectForEntityForName:@"Favorite" inManagedObjectContext:managedObjectContext];

// Step 2: Set Properties       
    newFavorite.name = @"Company";

    NSLog(@"%@",newFavorite);

// Step 3: Save Object

    NSError *error = nil;
    if (![newFavorite.managedObjectContext save:&error]) { // this is where the program crash
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

I am not sure what I'm doing wrong.

A: 

I am going to guess you have a UI element, such as a table, that activates a fetch when the UI is changed. For example, if you have a fetched results controller, any scrolling of the table can activate the fetched results controller's fetch.

You can't mutate a collection while iterating over that collection because the count of the iteration changes while the iteration is in process. A fetch iterates over the collection of objects matching its entity and predicate. If you insert an object while the fetch is working you will get an error.

Usually you see this problem with multiple threads but I think the UI might trigger the problem in the right set of circumstances.

TechZen
Yes I do have tableview that fetch the data. I want to save or copy over that data to another manageObjectContext. How can I do that without fetching the data?
You must be doing something unusual to trigger to the table update at the same time as the fetch. Is the UI element that triggers the insertion of the new object itself embedded in the tableview? If you have two or more context, their individual changes may be colliding with one another. I can't say without knowing more about your app.
TechZen
A: 

Im not sure what im doing that's unusual. Im creating an iphone app. I have my first view with buttons that goes to the second view which fetched the data and display it in a tableview. When user choose a particular cell it takes him to the detail(third view) of the data which is fetched and displays it. When click on button called 'favorite', it has to save that fetched detail data to the nsmanagedobjectcontext in a different entity, where it goes to another view, favorite. But when the button is clicked it terminates with the above error. Its similar when u click a button to add a new data im just saving over the same data to a different entity. I checked I dont have more then one context, i pass the context from one view to another. Please advice if Im doing something not right.. Thank you.

I have found the cause of crashing but i don't why. I removed my database from the resources and the save method is working. It creates its own db based on the xcdatamodel. But what is wrong with my database that its terminating with the above error. Please guide.