开发者

Core Data - inverse delete rules ... which one should I apply?

开发者 https://www.devze.com 2023-03-22 03:45 出处:网络
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开发者_C百科 Manager-to-Employee re

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开发者_C百科 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?


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

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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消