small medium large xlarge

Back to: All Forums  Core Data
Profilbild_bernd_rabe_pragsmall
21 Apr 2010, 07:29
Bernd Rabe (23 posts)

I got stuck in an app where I have to prepare a large amount of data depending on various user settings. Prepared data must be presented to the user (and store for later use in the persistent store), so I decided to use a background process, where I prepare a bunch of data before I load the new view and continue to prepare the rest in background.

During the background process several new entities inserted in the managed object context created in the background process, but have relationship entries to the managed object context in the main thread. I passed on entity to the background process and it seems, that initializing properties(not relationships) of the new entity with data from passed entity is possible but if you try to set a relationship the app crashes. I haven’t found any glue to fix that problem.

Any help is highly appreciated

P.S. I’ve stored a datamodel on http://public.me.com/b.rabe/de/DataModel.png It’s not the complete model but shows the most important part. Cycle is created in background and I tried to pass a LicenceExamDetails Entity.

Avatarsmall_pragsmall
21 Apr 2010, 16:03
Marcus S. Zarra (284 posts)

Passing @NSManagedObject@ instances between threads is not supported at all and I suspect that is the root of your problem. If you want to update a @NSManagedObject@ (whether it be properties or relationships) on more than one thread you need to fetch that object on each thread using that thread’s @NSManagedObjectContext@. In addition it is recommended that your main @NSManagedObjectContext@ listen for save notifications from the other @NSManagedObjectContext@ instances so that it can properly refresh itself.

Profilbild_bernd_rabe_pragsmall
21 Apr 2010, 17:16
Bernd Rabe (23 posts)

Thanks for the quick reply. Meanwhile (after reading the core data doc’s even more carefully) I found that passing the NSManagedObjectID is save (as long as it is a permanent id) and you can grab the object you need again in the backgrounds managed object context by objectWithID:

Avatarsmall_pragsmall
21 Apr 2010, 17:34
Marcus S. Zarra (284 posts)

Correct (which is discussed in the multi-threading chapter of the book). When you do that, a second “copy” of that @NSManagedObject@ is created from the second @NSManagedObjectContext@.

You must be logged in to comment