Re: [CoreData] Background Insertion
Re: [CoreData] Background Insertion
- Subject: Re: [CoreData] Background Insertion
- From: Andrew Kimpton <email@hidden>
- Date: Mon, 4 Jun 2007 22:23:14 -0400
On Jun 4, 2007, at 9:15 PM, Dave Hayden wrote:
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.
I can understand that might work - but it requires that someone
'know' about all the object controller instances in use in the app -
which is a bit of a cumbersome responsibility. One of the nice things
about CoreData is that you can 'delegate' the responsibility of
managing the collections to instances of object controllers which you
specifically don't need to know about and can just bind to.
I'll accept it if it is indeed the only way to fix the problem -
however I'm a little concerned that calling prepareContent is perhaps
just triggering something in the ManagedObjectContext and Persistent
store which is current hidden to me. After all I 'know' that the
object in question has been written to disk (I just got a save
completed notification), and I have an Object ID in hand, yet when I
ask a managedObjectContext that was linked to the store prior to the
save taking place to retrieve the object in question I get nil !
Seems a little odd...
Andrew 8-)
_______________________________________________
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