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?
I have a 1-to-many relationship in my example app, taken from the Core Data
Share
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
ManagerclassesremoveEmployeeObject:andremovedEmployeeObjects:method that tells theManagerinstances to delete itself if theemployeesrelationship is empty.You can also use validation methods for this or the
willSavemethods.