Reputation: 387
My app uses a UIManagedDocument to handle its data with Core Data. There's a to-many relationship between 2 entities: Post and Tag.
When I want to edit a post, I create a temporary NSManagedObjectContext, set its parentContext to the UIManagedDocument's managedObjectContext, and retrieve the post using the objectWithID: method.
Let's assume this:
Here's the problem:
All of post2 properties are properly retrieved, except for the to-many relationship tags.
Why does post2.tags have no tag?
Upvotes: 1
Views: 572
Reputation: 1837
Upon further testing, I think this is related to temporary vs. permanent object ids; if we're seeing the same thing, then I'd hazard a guess that the objects returned by the to-many relationship in MOC1 will have temporary, rather than permanent, ids.
Presumably in MOC2, you're doing some inserts to create the objects in the to-many relationship. When you're done inserting, and just prior to saving, obtain an array of objects that were inserted via [[moc2 insertedObjects] allObjects]. Hand this array to [moc2 obtainPermanentIDsForObjects:error] using the tracking array. Then call save on MOC2.
This seems to work for me. It appears that by default, calling save on MOC2 doesn't actually cause permanent object IDs to be created in MOC1.
Upvotes: 2
Reputation: 1837
I wish I had an answer for this, but so far as I've been able to determine, it's a bug. To-many relationship retrieval just doesn't seem work at all in this scenario.
Frankly, it seems a bit hard to believe this would have escaped testing, but I've been completely unable to make this work in any way.
Upvotes: 0