Basic data - reverse deletion rules ... what should I apply to?

I have a 1-to-many ratio in my sample application, taken from the Core Data documentation, where one manager has several employees. I get information on how to set a rule to delete relations between a manager and an employee, but what about an Employee-to-Manager relationship? If I need a case where, if ALL employees have been deleted, I want the dispatcher to be deleted as well, which deletion rule should be applied? The cascade does not make sense, because then, if one employee is removed, the manager will be deleted, even if he has other employees who are still connected. Nullify will correctly delete the relationship, but the Manager will not delete when the last employee was deleted. Am I missing something, or do I need to do something normal in this case?

+4
source share
1 answer

The delete rules are not specific enough to say: "delete self if the" bobs "relationships contain less than the" x "objects."

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

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

+3
source

All Articles