Re: [CoreData] Background Insertion
Re: [CoreData] Background Insertion
- Subject: Re: [CoreData] Background Insertion
- From: Andrew Kimpton <email@hidden>
- Date: Tue, 5 Jun 2007 07:05:41 -0400
On Jun 4, 2007, at 6:23 PM, I 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.
Am I missing something in how to co-ordinate the two
managedObjectContexts so that all the items inserted in one appear
in the other ? I've tried settings things like the merge policy
(store trumps objects) and also the setRetainsObjects flag on the
managedObjectContexts to no avail.
I know that my data has 'hit the disk' since if I quit and relaunch
the app then the array controllers and bound items all have the
appropriate data and displays. It's only the 'initial import' into
an empty persistent store and managedObjectContext that seems
troublesome.
I took out the calls to create new thread objects and just let the
parsing process block my UI. I still follow the 'rules' for multi-
threaded coredata apps and create a second ManagedObjectContext to
take the results of the parsing though.
This doesn't change anything in the behaviour of my application. The
results of parsing are still not reflected in the UI, controllers
don't update etc.
A second change I made was that since I was no longer using a
separate thread I didn't need the additional ManagedObjectContext and
instead could just use the same one as the main thread. Making this
change fixes the problem and my controllers etc all update. However
this is not really what I want but it does at least confirm that my
controllers are bound correctly (I think).
So my current feeling is that inserting objects into one
NSManagedObjectContext and then trying to reflect the new insertions
in UI bound to another requires something of a special approach that
I can't seem to work out !? I've looked at the BackgroundFetching
example app and I think I'm doing all the same things - but I am
inserting not fetching so there are some obvious differences (and I
note that BackgroundFetching actually uses an entirely seperate
command line app to generate the data - I'm not sure if that's to be
illustrative of CLI apps and CoreData or to avoid over complicating
the UI based sample or - dare I say - because there are issues with
inserting into a second managed object context?)
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