核心数据中的可选关系(删除和检查)
我有一个包含 2 个实体的核心数据模型:游戏和得分表。游戏与 ScoreTable 之间存在可选关系。我通常通过以下方式检查游戏是否有 ScoreTable:
NSManagedObject *scoreTable = [myGame valueForKey: @"scoreTable"];
if (scoreTable == nil) {
// wtv
}
当我想从游戏中删除 ScoreTable 时,我只会
[context deleteObject: scoreTable];
但是下次我检查 ScoreTable == nil 时,它似乎“停止”为零,好像那里有什么东西,但是空的,或者什么东西。所以,我正在做的是:
[myGame setValue: nil forKey: @"scoreTable"];
不知怎的,这感觉不对。或者确实如此?我不确定是否应该检查 scoreTable == nil
。还有另一种方法来检查那里是否有物体吗?
I have a Core Data model with 2 entities: Game 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 nil 检查失败,听起来好像您没有从 ScoreTable 返回到游戏设置逆关系。如果缺少逆元,那么当您删除 ScoreTable 核心数据时,无法清除与该对象的任何悬空关系。
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.