Core Data managed object does not see related objects until restart Simulator
Asked Answered
A

2

7

Got a Stumper (at least for me).

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

I have an Entity (Group) with a to-many relationship (called people) to entity (Person). When I add a new Group, and add then a new Person (setting the person's .group relationship to the new group), I cannot retrieve the related people using a predicate on the Person entity where ("group == %@", myGroup). I also tried using the Group's addPerson setter.

If I shut down XCode simulator and rerun it, it recognizes the relationship that was 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 to without shutting down the Simulator (or device if I'm running on device) in order to see the relationship.

If I do a [group.people count], immediately after adding the new Group and a related Person, it gives me the correct number. But a fetch with a predicate doesn't work until I restart the app.

It seems as if the managedObjectContext of the UIManagedDocument is not seeing the relationship. I've tried saving the context, saving context.parentContext, and saving the Document. None of that helped.

Any ideas would be appreciated!

Anklet answered 24/2, 2012 at 19:54 Comment(2)
Have you set the reverse relationship in your model?Unchain
Yes. I have a one-to-many from the Group to Person, named people. And an inverse one-to-one from Person to Group, named group.Anklet
C
10

A UIManagedDocument will save, well, basically, when it feels like it; you don't have control as to when that occurs. It will, however, certainly save on application termination, which is why you see the inserts when you restart.

As a result, while you may think you have permanent object ids, they're actually likely temporary, and thus can't be fetched. Simply dumping them via an NSLog would validate this, as temporary object ids show up as such when logged.

To force them to be permanent and thus usable, try the following after performing your additions. Assuming you have a UIManagedDocument as an 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'd replace the error handling with something a bit better here. The UIManagedDocument will now save at some point in the future (again, you have absolutely no control over when that'll happen, we're just asking it to do so in the last line, in the same way that an undo manager would), but newly-inserted objects should now have usable permanent ids and fetches should work properly.

And yes, this seems a bit odd to me too, but this appears to be the correct way to do things with a UIManagedDocument in play.

Frankly, I'd love for someone to tell me I'm incorrect and provide a better solution.

Capriole answered 26/2, 2012 at 7:47 Comment(6)
Allan, THANK YOU!!! This has cost me many hours or spinning my wheels. As you said, this doesn't seem to be a 'required' step from all of the tutorials and instructions I've read, but it is making my app work properly.Anklet
If somebody has any idea of how we can prevent needing to use this, I'd be glad to hear it as well.Anklet
I think it's somewhat telling that there is as of yet no Apple-provided sample code for UIManagedDocument. I think this may be an unintended bug introduced as a result of the new nested managed object contexts, and they're waiting to fix it before providing sample code. One nerd's theory, anyway.Capriole
Also note carefully, if you read the documentation long enough, you'll happen upon the single sentence indicating that updateChangeCount: is in fact the one and only correct way to save a managed document; saving the contexts themselves isn't the answer...learn from my fail here, just use updateChangeCount:.Capriole
Is calling saveToURL:forSaveOperation:completionHandler: on the UIManagedDocument equivalent to this method?Dekameter
@offex, no, not in my experience. The saveToURL... method is useful to perform a save and, via the completion handler, know when it completed, but it doesn't seem to as of this writing solve the temporary id problem. What seems to be happening is that so long as the containing object is referenced by anything, the children never become permanent unless they're forced to be. If you can remove all references and refetch, that's one option, but often one that's difficult to implement in practice.Capriole
J
-1

I was having a similar issue, the perform update didn't help me. This did:

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

I run this after I've inserted the objects and before I've saved.

Janeljanela answered 9/5, 2012 at 0:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.