The main data-driven object does not see related objects until it restarts. Simulator

Got a stumper (at least for me).

I am using iOS 5.0 w / ARC and Core Data inside a UIManagedDocument.

I have an Entity (Group) with a to-many relation (called people) to an object (Person). When I add a new group and add a new face (by setting the relation of the .group group to the new group), I cannot get related people using the predicate in the Person object, where ("group ==% @", myGroup). I also tried using the setperter of the AddPerson group.

If I close the Xcode simulator and rerun it, it recognizes the connection created in the previous run, I can even add new people to the existing Group object. I just can’t add a new group and then add people without closing the Simulator (or device, if I'm running on the device) to see the relationship.

If I do [group.people count], right after adding a new group and its associated Person, it gives me the correct number. But predicate fetching doesn't work until I restart the application.

It seems that managedObjectContext UIManagedDocument does not see the relationship. I tried to save the context by saving context.parentContext and saving the document. None of this helped.

Any ideas would be appreciated!

+7
source share
2 answers

UIManagedDocument will save, well, basically when it looks like; you have no control over when this will happen. However, it will certainly save when the application terminates, so you will see inserts on reboot.

As a result, although you may think that you have persistent object identifiers, they are actually probably temporary and therefore cannot be retrieved. Just dropping them through NSLog will verify this, as temporary object identifiers are displayed as such upon registration.

To make them permanent and therefore used, try the following after completing your add-ons. Assuming you have a UIManagedDocument as ivar:

- (void)performUpdate { NSManagedObjectContext * context = self.managedDocument.managedObjectContext; NSSet * inserts = [context insertedObjects]; if ([inserts count]) { NSError * error = nil; if ([context obtainPermanentIDsForObjects:[inserts allObjects] error:&error] == NO) { NSLog(@"BAM! %@", error); } } [self.managedDocument updateChangeCount:UIDocumentChangeDone]; } 

Obviously, you would replace error handling with something better. UIManagedDocument will now save at some point in the future (again, you have absolutely no control over when this will happen, we just ask him to do it on the last line, just like the undo manager), but the newly inserted objects now must have usable persistent identifiers, and selections should work as expected.

And yes, that seems a little strange to me, but that seems to be the right way to do something with the UIManagedDocument in the game.

Honestly, I would like someone to tell me that I am wrong and offer the best solution.

+10
source

I had a similar problem, the update did not help me. It happened:

 NSArray *insertedObjs = [[self.managedObjectContext insertedObjects] allObjects]; if ([insertedObjs count] > 0) { NSError * error; [self.managedObjectContext obtainPermanentIDsForObjects:insertedObjs error:&error]; } 

I run this after I inserted the objects and before I saved them.

-one
source

All Articles