0

How do I handle the deletion of the following objects? What should my delete rules look like?

Here's what my object graph looks like:

Boss

  • Boss-Department has a many-to-many-relationship
  • If a Boss is deleted, the Departments belonging to that Boss should NOT be deleted (but the Departments' relationship to this Boss should be deleted)

Department

  • Department-Employee has a many-to-many relationship
  • Department-Boss has a many-to-many relationship
  • If a Department is deleted, the Employees belonging to that Department should be deleted IF they don't have a relationship to any other Department

Employee

  • Employee-Department has a many-to-many relationship
  • Employees will never be deleted directly (only through the deletion of a department). Oh happy life!
Peter Warbo
  • 11,136
  • 14
  • 98
  • 193

1 Answers1

0

In this situation the Delete rules for your objects should be handled like this

Boss-Department -> Nullify

Department-Employee -> Deny

Department-Boss -> Nullify

Here is what Apple explains the Delete Rules as

Deny: If there is at least one object at the relationship destination, then the source object cannot be deleted. For example, if you want to remove a department, you must ensure that all the employees in that department are first transferred elsewhere (or fired!) otherwise the department cannot be deleted.

Nullify: Set the inverse relationship for objects at the destination to null. For example, if you delete a department, set the department for all the current members to null. This only makes sense if the department relationship for an employee is optional, or if you ensure that you set a new department for each of the employees before the next save operation.

Cascade: Delete the objects at the destination of the relationship. For example, if you delete a department, fire all the employees in that department at the same time.

No Action: Do nothing to the object at the destination of the relationship. For example, if you delete a department, leave all the employees as they are, even if they still believe they belong to that department.

For more information check out the CoreData Programming Guide

MobileOverlord
  • 4,580
  • 3
  • 22
  • 30