Re: CoreData - trying to avoid separate thread
Re: CoreData - trying to avoid separate thread
- Subject: Re: CoreData - trying to avoid separate thread
- From: Chris Suter <email@hidden>
- Date: Fri, 5 Jan 2007 09:03:33 +1100
On 05/01/2007, at 8:41 AM, Richard Salvatierra wrote:
The problem is I am creating new objects and inserting them into a
managedObjectContext.
([NSEntityDescription insertNewObjectForEntityForName: @"Media"
inManagedObjectContext: managedObjectContext])
My understanding, and findings, is that CoreData is not thread
safe. I have a text field bound to an arrayController with a
displayPattern that will show the count of items in that
arrayController. This does not update properly if my import
routine is in a separate thread. It leaves off the last item (for
a few seconds anyway, then updates properly) Unless there is a
way to force the controller to update properly. ArrangeObjects and
RearrangeObjects does not do the job.
Sure, but what I meant was that you should do everything that is
thread safe in a separate thread, i.e. the reading of the files and
then anything that isn't thread safe on the main thread. So in your
separate thread you'd have something like:
- (void)myThread:(id)arg
{
...
while (somethingToDo) {
// Read item of data from file
...
// Process item on main thread
[self performSelectorOnMainThread:@selector (insertOnMainThread:)
withObject:itemRead
waitUntilDone:NO];
...
}
}
- (void)insertOnMainThread:(id)data
{
// Insert the data here
...
}
If you've got a lot of things to read and you find it's slow, you can
try batching them up.
You should also read:
http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/
Articles/cdMultiThreading.html
which discusses alternative approaches.
- Chris
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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