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.
Allan bazinet
source share