Re: [CoreData] Background Insertion
Re: [CoreData] Background Insertion
- Subject: Re: [CoreData] Background Insertion
- From: Dave Hayden <email@hidden>
- Date: Mon, 4 Jun 2007 18:15:28 -0700
On Jun 4, 2007, at 3:23 PM, Andrew Kimpton wrote:
My application needs to insert a several hundred entities into a
CoreData store as a result of parsing an XML file. In order to not
'block' the main UI thread I create a second thread and in that
thread create a second NSManagedObjectContext (for the same
NSPersistentStoreCoordinator as the main thread). My parsing and
insertions seem to go fine and I can save the managedObjectContext
at the end of parsing without error.
However the NSArrayController(s) and other bound items in the UI
don't seem to reflect the newly inserted items.
If I handle the NSManagedObjectContextDidSaveNotification in the
worker thread and use it to create an array of object id's for the
newly inserted items when I enumerate that array in my main thread
(and with the main threads ManagedObjectContext) using
objectRegisteredForID the returned object for each ID is always nil.
If I run my import a second time so that I'm updating rather than
inserting a lot of objects the same code gives me valid object
pointers.
I've been trying to work this out for the last couple days myself. A
lot of what I've read suggests that simply doing objectWithID: in the
main thread will cause the object to appear in the UI, but I've never
been able to get that to work. What does seem to work is calling
[arrayController prepareContent] or [arrayController fetch:nil] in the
main thread after the data is saved in the worker thread.
Unfortunately, when I do this in 10.5 with garbage collection on it
crashes on the third or fourth call, so I don't know whether this
really isn't the right way to update the UI or there's a bug under the
hood.
-D
_______________________________________________
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