How to delete an object when it is deleted from the array controller?

I have an object (e.g. Employee) in a managed object model that is linked to two other objects (e.g. Department and team). Both relationships are β€œone to many” (i.e., the Employee must have one Department, and there are many employees in one team, teams and departments). These two may or may not coincide (for example, a team may consist of HR, Accounting, and IT employees, or it may consist of several employees from the same department).

Department <-->> Employee <<--> Team 

I have two NSArrayControllers that provide data for two NSTableViews, Department tables and Team tables. Employees can move between departments and between teams without any problems, but I'm not sure how to remove (start) an employee.

If I send a delete message to any of the array controllers, the employee will be removed from the command (for example), but left in the department, and the object graph is in an inconsistent state. Even if I call the remove action on both controllers, the object is not deleted - it is lost and just freezes in uncertainty.

Initially, I had a relationship between the department and the team (Employee entities) set to the Nullify deletion rule, but even changing one or both to the cascade does not help.

Do I need to redefine the remove: action on array controllers to actually remove the employee, or am I missing something really obvious?

+4
source share
1 answer

NSArrayController has two different behaviors when you use Core Data. If it is configured to simply retrieve objects directly from the context of the managed object, it will delete the objects when they are deleted.

If you bind the contentSet to another controller, for example, it looks like you are in this case, the default behavior is simply to remove the object from the relation. However, if you want to delete it, there is the option "delete the object when deleting", which will lead to the desired result.

+9
source

All Articles