Re: Core Data Concurrency Issues
Re: Core Data Concurrency Issues
- Subject: Re: Core Data Concurrency Issues
- From: Heath Borders <email@hidden>
- Date: Thu, 04 Aug 2011 18:48:11 -0500
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" <email@hidden> 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 (email@hidden)
>
> 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:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden