A managed object context needs to be used and created from the same thread. Create and use childContext in a single dispatchAsync block.
-Heath On Aug 4, 2011 5:32 PM, "Jeff Kelley" <slauncha...@gmail.com> wrote: > I’m having some issues with concurrency with Core Data. I create an object > that has a to-many relationship with another object (which in turn has a > to-one reciprocal relationship with the first object), then dispatch_async > onto a private queue. In that queue, I use a separate managed object > context, which I’ve created earlier and only use within that queue. Here’s a > snippet: > > ParentObject *parent = [NSEntityDescription > insertNewObjectForEntityName:@"ParentObject"] > inContext:[self moc]]; > > NSManagedObjectID *objectID = [parent objectID]; > > dispatch_async(dedicatedQueue, ^{ > // Note: childContext is an NSManagedObjectContext that has been created > earlier and is only used within this queue. > ParentObject *parent = [childContext objectWithID:userID]; > > for (NSDictionary *jsonDict in allChildDicts) { > ChildObject *child = [NSEntityDescription > insertNewObjectForEntityName:@"ChildObject"] > inContext:childContext]; > [parent addChildObject:child]; > } > }); > > > If I try to save the context within the dedicated queue, I get validation > errors on the child objects with the text “Dangling reference to an invalid > object.” > > It appears to work correctly if I create the parent object within the > dedicated child queue. So, what is the proper way to create these objects on > the main queue and then update them in the background? Thanks in advance. > > Jeff Kelley > _______________________________________________ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > Please 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: > http://lists.apple.com/mailman/options/cocoa-dev/heath.borders%40gmail.com > > This email sent to heath.bord...@gmail.com _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com