Why might changes be populated from one NSManagedObjectContext to another without an explicit merge?
I'm working on an object import feature that utilizes multiple threads/NSManagedObjectContexts, using http://www.mac-developer-network.com/columns/coredata/may2009/ as my guide (note that I am developing for iPhone).
For some reason, when I save one of my contexts the other is immediately updated with the changes, even though I have commented out my calls to mergeChangesFromContextDidSaveNotification. Are there any reasons the contexts might be merging into one another without an explicit call?
Here a log of what's going on:
// 1.) Main context is saved with "Peter Gabriel"
// 2.) Test context is created, begins with same contents as main context
// 3.) Main context is inserted with "Spoon"
// 4.) Test context is inserted with "Phoenix"
// Contents at this point:
CoreTest[4341:903] Artists in main context:开发者_如何转开发 (
"Peter Gabriel",
"Spoon"
)
CoreTest[4341:903] Artists in test context: (
"Peter Gabriel",
"Phoenix"
)
// 5.) testContext is saved
// New contents of contexts:
CoreTest[4341:903] Artists in main context: (
"Peter Gabriel",
"Phoenix",
"Spoon"
)
CoreTest[4341:903] Artists in test context: (
"Peter Gabriel",
"Phoenix"
)
As you can see, the test context is saved midway through, and the main context suddenly has the new objects from the test context, even though I haven't performed the whole NSManagedObjectContextDidSaveNotification/mergeChangesFromContext combo.
My understanding is that no changes will ever be merged unless done so explicitly... does anyone know what's going on here?
My understanding is that no changes will ever be merged unless done so explicitly...
That's not correct. I guess after your 5)
you refetched Artists
in the main context, right? Any fetch always goes down to the disk, accessing the file. If that's after the save from a different context, that'll fetch new entries you just created. If there're conflicts, those will be dealt with according to your mergePolicy
, see this Apple doc.
精彩评论