I have an indexing process which runs on a background thread and indexes into a 
CoreData database. I experienced some odd crashes until I properly read the 
documentation and understood that sharing an NSManagedObjectContext across 
threads was a bad idea, and I've fixed the code so that the indexer has it's 
own managed object context. All is well. 

Now I want to keep things in sync so that when the indexer finishes and saves, 
the main thread context is updated. I found the notification which is sent when 
a managed object context is saved (NSManagedObjectContextDidSaveNotification) 
and what I want to do is use mergeChangesFromContextDidSaveNotification: to 
update any other contexts on any other threads. 

Checking the documentation for NSNotificationCenter however I see that the 
notifications are processed in the thread in which they were sent, that of 
course will be my indexing thread and that's exactly what I was trying to 
avoid, updating contexts across threads. What's the best way to deal with this? 
I'm assuming there is no guarantee that the mergeChanges... is atomic in any 
way, I'm quite sure it's not. 

I could throw the updates onto the main thread, but that won't help me if the 
update was on the main thread and the indexer needs it. I could lock and unlock 
the context around the mergeChanges.. call but I think for that to work I need 
to use a context lock/unlock around every single context access anywhere in the 
code, that may well be something I have to live with in order to be 
multi-threaded but if there's something I'm missing what is it? 

One last idea, is there a way to just tell an NSManagedObjectContext that it's 
now dirty and it should reload everything the next request it has? I know you 
can do it for individual objects, but is it possible to just tell a whole 
context to go re-read everything? That might work better than merging (as 
actually many of the contexts are really used in a read-only manner). 
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to