views:

141

answers:

1

I have a Table View that fetches data from Core Data using FetchedResultsController and it registers for the data update.

On a second thread, I download the data from the server and update the same data (that's used by the Table View). The update is not complicated: it is just updating a BOOL field of the entity.

When I call the save on Object Context, I get this exception: NSInternalInconsistencyException. And the reason is "Failed to process pending changes before save. The context is still dirty after 100 attempts. ..." If I do not save right after the update but only at the time when the application is about to terminate, the application runs fine and the Table View is correctly updated and the data is persisted.

Any pointer on why that might be happening? Am I doing something wrong?

A: 

Managed object contexts are not thread safe. Do you have a separate MOC for each thread?

If so, I believe the correct pattern is to register for NSManagedObjectDidSaveNotifications from the background MOC such that you can do a mergeChangesFromContextDidSaveNotification on the main MOC (from the main thread). This will keep your MOCs in sync; it does not happen automatically.

admanimal
Oh...I'm keeping a singleton MOC from a sort-of-like CoreDataUtil class. Now feeling stupid. @_@One question: changes saved from one MOC will be reflected in another MOC? Thanks!!!
Justin
Added a bit more info
admanimal