Re: (Inverse) relationship lost after being set properly
Re: (Inverse) relationship lost after being set properly
- Subject: Re: (Inverse) relationship lost after being set properly
- From: Doug Knowles <email@hidden>
- Date: Fri, 24 Jun 2005 22:17:23 -0400
Found it. In moving managed objects from one "owner" to another, I
was simply adding the item to the new owner's mutable set
(representing the to-many relationship), without removing it from the
old owner's set. As a result, when I deleted the former owner, Core
Data thought the objects still belonged to it, and nullified the
inverse (to-one) relationship, as specified by my model.
The correct behavior (at least what's working for me now) is to remove
the object from the old owner's set before adding it to the new owner;
something like:
oldOwnersSet = [oldOwner mutableSetValueForKey:@"myToManyRelationship"];
newOwnersSet = [newOwner mutableSetValueForKey:@"myToManyRelationship"];
[oldOwnersSet removeObject:objBeingMoved]; // I was omitting this <<<<<<<<<<<
[newOwnersSet addObject:objBeingMoved];
Pretty fundamental, but maybe documenting my mistake will save
somebody else some time later.
Doug K;
On 6/24/05, Doug Knowles <email@hidden> wrote:
> I have a Core Data application that constructs a hierarchy of managed
> objects from an XML payload. The objects have a conventional tree
> relationship (as you might expect from XML data): a "parent" has a
> to-many relationship to the "children", which have an inverse to-one
> relationship to the parent. The construction of the tree of managed
> objects from the XML appears to work fine.
>
> The application occasionally re-loads the XML data, during which it
> fetches a new XML payload and adds the new "children" to the original
> "parent". I'm using mutableSetValueForKey and addObject to "move" the
> new children to the old parent. I have confirmed (via NSLog debug
> statements) that the children's "parent" reference is being correctly
> updated when they are added to the "old" parent's mutableSet.
>
> However, shortly thereafter, if I examine the "new" children, their
> "parent" entity references are now nil; these references are somehow
> being reset to a bad value (nil) after I confirm that they have been
> correctly updated.
>
> Does this ring a bell for anyone? I have read threads where the
> inverse relationship spec is sometimes broken, but I don't think
> that's the problem here, since the children's to-one "parent"
> attribute is shown to be properly updated after the parent's to-many
> "child" attribute is modified; the damage appears to be inflicted
> later.
>
> Thanks in advance,
>
> Doug Knowles
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden