My app maintains two NSManagedObjectContexts. One runs on the main thread, the other processes network events and runs on the background thread.
Whenever a network event is received:
- The main MOC does a save
- The background thread merges the changes
- The network event is processed and changes are applied to the background MOC
- The background MOC saves.
- The main MOC merges the changes.
This works fine until I try to delete an NSManagedObject inside the main thread:
a. prepareForDeletion gets called again on the background thread after step #2
b. prepareForDeletion called on main thread after step #5
What’s worse EVERY TIME either MOC saves/merges abominations a. and b. rear their ugly head.
As far as I can tell, the NSManagedObject is being resurrected then deleted, over and over again.
I’m tearing out my hair at this problem. I’ve spent the last week fighting to get multiple MOCs running. I had no idea that something so simple would cause so much grief.
Lorean,
My advice is to not maintain the background MOC. Rather, always create it as you start your background processing. That way it is always in the same state as the persistent store. (The row cache is your friend. Don’t worry so much about the state of the background MOC.)
Andrew