Re: Core data fetch and multithreading
Re: Core data fetch and multithreading
- Subject: Re: Core data fetch and multithreading
- From: Chris Hanson <email@hidden>
- Date: Mon, 22 Nov 2010 22:31:20 -0800
On Nov 21, 2010, at 11:47 AM, vincent habchi <email@hidden> wrote:
> briefly speaking, I have a Core Data Entity bearing a to-many relationship (therefore an NSSet * iVar). A dialog on the main thread can modify this set, while it may be simultaneously enumerated on a background GCD thread. I have therefore used the managed object context provided mutex to protect the respective snippets.
Don't do this. If you're using bindings or KVO at all in your main thread, you CANNOT lock your context every place you need to in order to make this safe. Furthermore, I suspect you may not be locking your context on the background thread because you're "just reading" - that's also incorrect.
Use a separate context on the background thread, attached to the same NSPersistentStoreCoordinator, and only pass managed object IDs between threads. Also, be sure to create your background thread's context on the thread or queue which you wish to use it; don't create one on the main thread and then pass it in to your background thread. (The reason for is is that a context can care about eg the run loop it's on. You don't want to use a context attached to the main thread's run loop from a background thread.)
This is pretty much the standard for Core Data multithreading: thread isolation of object graphs (as represented by contexts), passing only object IDs( and did-save notifications containing object IDs) between threads.
-- Chris
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden