核心数据中的可选关系(删除和检查)

发布于 2024-08-21 03:55:41 字数 554 浏览 3 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人哭 2024-08-28 03:55:41

如果 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文