gpt4 book ai didi

objective-c - 核心数据多对多关系的正确删除规则?

转载 作者:搜寻专家 更新时间:2023-10-30 20:00:08 24 4
gpt4 key购买 nike

在我的 Core Data 模型中,我有一个具有多个多对多关系的 Profile 实体。其中之一是“寻找”。许多配置文件可能正在寻找许多不同的东西。

Core Data 中的关系将其删除规则设置为无效。我想要发生的是,当一个配置文件被删除时,所有的寻找条目不受影响,当一个寻找条目被删除时,它应该从每个人的配置文件中删除。

这似乎我应该对 Profile 和 Looking For 之间的关系的两个方向不采取任何行动,但是通过谷歌搜索和阅读文档,我不确定这是正确的,事实上 No Action 似乎是某种东西除非您进行大量优化,否则您几乎永远不想使用。这是文档:

No Action Do nothing to the object at the destination of the relationship. For example, if you delete a department, leave all the employees as they are, even if they still believe they belong to that department.

It is less obvious why the No Action rule might be of use, since if you use it you have the possibility of leaving the object graph in an inconsistent state (employees having a relationship to a deleted department).

If you use the No Action rule, it is up to you to ensure that the consistency of the object graph is maintained. You are responsible for setting any inverse relationship to a meaningful value. This may be of benefit in a situation where you have a to-many relationship and there may be a large number of objects at the destination.

所以我觉得我应该改用 Nullify(我目前正在使用),但是 Nullify 的文档说:

Nullify Set the inverse relationship for objects at the destination to null. For example, if you delete a department, set the department for all the current members to null. This only makes sense if the department relationship for an employee is optional, or if you ensure that you set a new department for each of the employees before the next save operation.

现在,如所写,这似乎意味着如果您删除一个配置文件,则与该配置文件关联的每个寻找都将清空其与配置文件的关系,即从所有配置文件中删除。

在这种情况下正确的做法是什么?

最佳答案

“无效化”是正确的。假设个人资料 p1 与 Looking Fors l1l2l3 相关。如果 p1 被删除并且关系被设置为 Nullify,那么只有p1将从l1l2l3中的逆关系中移除。

如果使用“No Action”,l1l2l3 中的反向关系将保持不变,因此指向一个不存在的元素 p1。您将不得不删除 p1从这些对象中“手动”。

换句话说,“Nullify”是保持对象图一致的最简单规则:如果ab相关,并且b 被删除,则 b 不再与 a 相关。

关于objective-c - 核心数据多对多关系的正确删除规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19821945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com