Optional relationships in Core Data (deleting and checking)
I have a Core Data model with 2 entities: Game开发者_如何学C and ScoreTable. A Game has an optional relationship with ScoreTable. I usually check if a game has a ScoreTable by doing:
NSManagedObject *scoreTable = [myGame valueForKey: @"scoreTable"];
if (scoreTable == nil) {
// wtv
}
And when I want to delete a ScoreTable from a Game I'll just
[context deleteObject: scoreTable];
But the next time I check if the scoreTable == nil, it seems that it "stopped" being nil, as if there's something there, but empty, or something. So, what I'm doing is:
[myGame setValue: nil forKey: @"scoreTable"];
Somehow this doesn't feel right. Or does it? I'm not sure if I should check if the scoreTable == nil
. Is there another way of checking if there is an object there?
If the nil check is failing it sounds like you do not have a inverse relationship set up from scoreTable back to game. If the inverse is missing then when you delete scoreTable Core Data has no way to clean up any dangling relationships to that object.
精彩评论