iPhone核心数据:级联删除跨多对一关系 [英] iPhone Core Data: Cascading delete across a many-to-one relationship

查看:241
本文介绍了iPhone核心数据:级联删除跨多对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类A和B与从A到B的多对一关系(多个A对象可以引用同一个B)。问题是,如果A侧的删除规则是Cascade,则只有当最后引用A被删除时,才会删除B,否则将删除第一关联的A被删除。 B关系的删除规则是Nullify如果重要。

I have two classes A and B with a many-to-one relationship from A to B (multiple A objects may reference the same B). The question is, if the delete rule on the A side is Cascade, will B be deleted only when the last referencing A is deleted or will it be deleted the first time an associated A is deleted. The delete rule for the B side of the relationship is Nullify if that matters.

此外,我在Core Data文档中读到Optional标记在某些情况下很重要。但是不清楚他们说明的关系如何与我的情况有关。他们正在谈论一个收容案件(B由A拥有),而我的案例是订阅/关联(B与A相关)。

Also, I read in the Core Data docs that the Optional flag matters in some cases. But it wasn't clear how the relationships they were illustrating related to my case. They were talking about a containment case (B is owned by A) whereas my case is one of subscription/association (B is related to A).

删除程序在程序中的代码,但希望允许Core Data做正确的事情,如果可能的话。但是还不清楚我在寻找的垃圾收集语义是否支持Core Data。

I could simply manage deletion programmaticaly in the code but wanted to allow Core Data to do the right thing if possible. But it's not clear that the garbage collection semantics that I'm looking for are supported in Core Data.

有任何建议吗?

推荐答案

我有同样的目标,你显然已经删除(删除 B ,最后参考 A 。它花了我比预期要长得多。特别是因为

I had the same goal as you apparently had (delete B as soon as the last referenced A is deleted). It took me longer than expected to get this right. Particularly because


  • A 准备删除时, / strong>可能尚未更新,因此您不能只对 B 中引用的 A 进行计数。

  • isDeleted on A 似乎已在 -prepareForDeletion
  • 中设置
  • At the time A prepares for deletion, the to-many relationship in B might not be updated yet, so you can't just count the A referenced in B.
  • isDeleted on A seems to be already set during -prepareForDeletion

如果有人感兴趣(我会使用部门 < - > 员工,因为它更容易阅读),这对我有用:

Here's what worked for me if anybody's interested (I'll use Department <-->> Employee because it's easier to read):

员工

- (void)prepareForDeletion {
    // Delete our department if we we're the last employee associated with it.
    Department *department = self.department;
    if (department && (department.isDeleted == NO)) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isDeleted == NO"];
        NSSet *employees = [department.employees filteredSetUsingPredicate:predicate];

        if ([employees count] == 0) {           
            [self.managedObjectContext deleteObject:department];
        } 
    }
}

其他人建议逻辑到 -willSave 。我更喜欢上面的解决方案,因为我可能实际想要保存一个空部门在某些情况下(例如在手动存储迁移或数据导入期间)。

Other people have suggested putting this logic into -willSave in Department. I prefer the solution above since I might actually want to save an empty department in some cases (e.g. during manual store migration or data import).

这篇关于iPhone核心数据:级联删除跨多对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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