如何正确级联删除Core Data中的托管对象?
我有一个核心数据模型,它具有三个实体:A、B 和 C。A 与 B 具有一对多关系,B 与 C 具有多对多关系。A 的删除规则 -> ; B为“级联”,且B-> A 是“不采取行动”。 B的删除规则-> C是“无行动”,并且C-> B是“否认”。
我在对 A 实体执行删除时遇到问题。我想要发生的事情如下:
- 我删除 A 的一个实例(使用
deleteObject:
) - 删除会传播到与 A 关联的任何 B(由于“级联”删除规则)
- 所有 B 与 A 关联A 被删除
- 属于 C 且其关联的 B 被删除的任何关系也将被删除
这可能有点令人困惑,所以让我解释一下:当 A 被删除时,删除所有关联的 B。任何引用这些 B 的 C 都不能再引用它们。
在我的测试中,我根本没有看到“级联”删除规则对我有用。当我删除 A 时,我会立即调用 processPendingChanges(只是为了确保删除已完成)。然后我比较删除之前和之后 NSManagedObjectContext 中 A 和 B 的数量。 A的实例已被正确删除(A的总数现在比删除前少了1)。然而,B 的数量保持不变。因此,似乎“级联”删除规则没有得到遵守。
我知道我可以手动浏览 A -> B关系,并手动删除每个B。不过,这似乎是Core Data免费提供的东西,所以我不想这样做,除非Core Data不够。欢迎提供有关使用“级联”删除规则的任何信息。
I have a Core Data model which has three entities: A, B, and C. A has a one-to-many relationship with B, and B has a many-to-many relationship with C. The delete rule for A -> B is "Cascade", and B -> A is "No Action". The delete rule for B -> C is "No Action", and C -> B is "Deny".
I am having trouble performing a delete on the A entity. What I want to happen is the following:
- I delete an instance of A (using
deleteObject:
) - The delete propagates to any B's associated with A (due to the "Cascade" delete rule)
- All B's associated with A are deleted
- Any relationships belonging to C's whose associated B's were deleted, are also removed
That may be a little confusing, so let me paraphrase: When an A is deleted, delete all associated B's. And any C's which reference those B's must not reference them any more.
In my testing, I am not seeing the "Cascade" delete rule work for me at all. When I delete an A, I invoke processPendingChanges
immediately afterwards (just to make sure the deletion has been done). Then I compare the number of A's and B's that were in the NSManagedObjectContext before the deletion, and after it. The instance of A has been properly deleted, (the number of total A's is now one less than before the deletion). However, the number of B's remains the same. So, it seems that the "Cascade" delete rule is not being honored.
I know I can manually go through the A -> B relationship, and manually delete each B. However, it seems like this is something Core Data provides for free, so I don't want to do that unless Core Data is insufficient. Any information regarding the use of "Cascade" delete rules is welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我当然不是核心数据专家,但阅读 有关各种删除规则选项的文档,在我看来,您想要 B -> C关系是无效,而不是无行动。也许 B 不会消失,因为 C 仍然保留着对它们的引用?
I'm certainly no Core Data expert, but reading the documentation on the various delete rule options, it seems to me that you want the B -> C relationship to be Nullify, rather than No Action. Perhaps the Bs aren't going away because the Cs are still holding references to them?
基于
此外,如果B被删除,那么C也应该被删除。再次级联。
即。 B 与 A 的关系无效。 C与B的关系无效。这样当 C 被删除时,B 不会被删除。当B被删除时; A 未被删除。
正如这幅获胜图所示。
A--->>B 级联
<---- 取消
B--->>C 级联
<---- 无效
Based on the
Further, if B is removed, then C's should also be deleted. Again Cascade.
ie. Bs relationship to A is Nullify. C relationship to B is Nulllify. Such that when C is deleted, B is NOT deleted. And when B is deleted; A is NOT deleted.
As in this winning drawing.
A--->>B Cascade
<---- Nullify
B--->>C Cascade
<---- Nullify