如何正确级联删除Core Data中的托管对象? [英] How to properly cascade delete managed objects in Core Data?

查看:321
本文介绍了如何正确级联删除Core Data中的托管对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心数据模型,它有三个实体:A,B和C. A与B有一对多的关系,B与C有多对多关系。删除规则A - > B是级联,B - > A是无动作。 B - > C的删除规则为无操作,C - > B为拒绝。

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".

我无法对A实体执行删除。我想要发生的是以下:

I am having trouble performing a delete on the A entity. What I want to happen is the following:


  1. 我删除了一个A的实例(使用 deleteObject:

  2. 删除会传播到与A相关联的任何B(由于级联删除规则)

  3. A已删除

  4. 也删除属于其相关联的B的C的任何关系

  1. I delete an instance of A (using deleteObject:)
  2. The delete propagates to any B's associated with A (due to the "Cascade" delete rule)
  3. All B's associated with A are deleted
  4. Any relationships belonging to C's whose associated B's were deleted, are also removed

这可能有点混乱,所以让我解释:当A被删除,删除所有相关的B。任何引用这些B的C都不能再引用它们。

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.

在我的测试中,我没有看到Cascade删除规则对我有用。当我删除一个A,我立即调用 processPendingChanges (只是为了确保删除已经完成)。然后我比较删除之前和之后的NSManagedObjectContext中的A和B的数量。 A的实例已被正确删除,(总A的数目现在比删除之前少一个)。然而,B的数量保持不变。因此,似乎Cascade删除规则没有得到执行。

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.

我知道我可以手动通过A - > B关系,并手动删除每个B 。但是,看起来这是Core Data免费提供的,所以我不想这样做,除非Core Data不足。欢迎使用Cascade删除规则的任何信息。

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.

推荐答案

我当然不是核心数据专家, 关于各种删除规则选项的文档,在我看来,您希望B - > C关系 Nullify ,而不是无操作 。也许B不会离开,因为Cs仍在持有对他们的引用?

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?

这篇关于如何正确级联删除Core Data中的托管对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆