Re: Coredata and threads
Re: Coredata and threads
- Subject: Re: Coredata and threads
- From: Bill Bumgarner <email@hidden>
- Date: Wed, 4 Jan 2006 14:13:27 -0800
On Jan 4, 2006, at 1:52 PM, Todd Freese wrote:
Actually, I have a background thread that is importing objects. It
creates the objects and needs to add them to the store. The main
thread is displaying them. At no time do both threads access the
store. Just the main thread.
In this case, I am thinking I should be OK. The importer thread
would create a new context, add the obects, and call the main
thread to update.
Or am I missing something major here?
Interpretation: "Add to store" means "add objects to a managed
object context and save through 1 persistent store coordinator to 1
store".
You should never use a managed object context or persistent store
coordinator, directly or indirectly, from two different threads*.
There is no way to know or control what may happen within the AppKit
or CoreData -- there could be a timer, there may be an update caused
by user action (possibly in a different application). For example,
if the user scrolls a table view such that more fields become
visible, that may cause the controller to hit the managed object and
trigger faulting of other objects -- not only reading data, but also
modifying the object graph. If the background thread is also doing
the same, bad things are gonna happen. Likewise, as the bakground
thread imports objects, it is going to be causing state changes that
may cause key/value observer style change notifications to hit the
controller.
b.bum
*Of course, if you decide to manage the locking via the NSLocking API
provided on the managed object context and persistent store
coordinator yourself and you do so totally correctly, then you can
interleave access to the MOC and PSC between threads. I would
suggest against doing this.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden