核心数据 - 反删除规则...我应该应用哪一个? [英] Core Data - inverse delete rules ... which one should I apply?

查看:95
本文介绍了核心数据 - 反删除规则...我应该应用哪一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的示例应用程序中有一对一的关系,取自Core Data文档,其中一个经理有多个员工。我有关于如何设置Manager-to-Employee关系删除规则的部分,但关于Employee-to-Manager关系呢?如果我想要一个案例,如果所有的员工都被删除,我想管理器也被删除,我应该应用什么样的删除规则? Cascade没有意义,因为如果一个员工被删除,经理将被删除,即使他/她有其他员工仍然链接。 Nullify将正确删除关系,但在删除最后一个员工时不会删除Manager。
我缺少一些东西,或者在这种情况下我必须做一些自定义的东西吗?

I have a 1-to-many relationship in my example app, taken from the Core Data documentation, where one Manager has multiple employees. I get the part on how to set the Manager-to-Employee relationship delete rule, but what about the Employee-to-Manager relationship? If I want a case where, if ALL the employees have been deleted, I want the Manager to also be deleted, what kind of delete rule should I apply? Cascade doesn't make sense, because then if one employee is deleted, the manager will get deleted even though he/she has other employees still linked. Nullify will delete the relationships correctly, but it won't delete the Manager when the last employee has been deleted. Am I missing something, or do I have to do something custom in this case?

推荐答案

删除规则没有足够的特异性说:如果关系'bobs'包含少于'x 对象。

The delete rules don't have enough specificity to say, "delete self if relationship 'bobs' contains fewer than 'x' objects."

相反,您应该将此类业务逻辑放在自定义的NSManagedObject子类中。您可以在经理 removeEmployeeObject:中移除一个支票removeEmployeeObjects:方法,如果 employees 关系为空,它会告诉 Manager 实例删除自身。

Instead, you should put such business logic in a custom NSManagedObject subclass. You can put a check in the Manager classes removeEmployeeObject: and removedEmployeeObjects: method that tells the Manager instances to delete itself if the employees relationship is empty.

您也可以为此或 willSave 方法使用验证方法。

You can also use validation methods for this or the willSave methods.

这篇关于核心数据 - 反删除规则...我应该应用哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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