I was using eoSourceObject.takeValueForKey( destObject, relationshipKey ) to set a relationship. Upon saving the eoSourceObject had the corrected related object and the editing context state was correct with the relationship made, etc. I could navigate the EC and objects in the EC using the DebugMarker and ec examination pages in Project Wonder. SO basically as far as the EOF was concerned, all changes had been saved and the current state of the EC reflected the current state of the database.
However the foreign key was actually not getting updated in the eoSourceObject table. Logging SQL showed that indeed this was the case. The source object was written out to the database with the old foreign key even though the ec had the new related object!
Now digging a little deeper, the takeValueForKey was calling the auto-eogenerated code which in turn called takeStoredValueForKey (CTMediaTemplate is the destination object here and this method is in the eogenerated _CTCampaign, the source object.)
public void setMediaTemplate(CTMediaTemplate aValue) {
if( log.isDebugEnabled() ) log.debug( "updating mediaTemplate from "+mediaTemplate()+" to "+aValue );
takeStoredValueForKey(aValue, "mediaTemplate");
}
At face value, it seemed like everything was fine and confirmation pages of the saved state of the EC showed everything OK ..... but later once the snapshot expired, the snapshot got updated form the database and the OLD original foreign key showed up and then the wrong related destObject was displayed ..... AAARRRGGGHHH!!!
The SOLUTION was to change my code to use
eoSourceObject.addObjectToBothSidesOfRelationshipWithKey( destObject, relationshipKey);
.....but WHY does not takeValueForKey or takeStoredValueForKey not set the relationship both ways?
Kieran