Nick Locking
Nick Locking

Reputation: 2141

Correct Delete Rule for Core Data Many-to-Many Relationship?

In my Core Data model I have a Profile entity with a couple of Many-to-Many relationships. One of them is 'Looking For'. Many profiles can be looking for many different things.

The relationships in Core Data have their Delete Rule set to Nullify. What I want to happen is, when a Profile is deleted, all the Looking For entries remain unaffected, and when a Looking For entry is deleted, it should be removed from everyone's Profiles.

This seems like I should be doing No Action for both directions of the relationship between Profile and Looking For, but from googling around and reading docs, I'm not sure this is correct, and in fact No Action seems to be something you would almost never want to use unless you were heavily optimizing. Here's the documentation:

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.

It is less obvious why the No Action rule might be of use, since if you use it you have the possibility of leaving the object graph in an inconsistent state (employees having a relationship to a deleted department).

If you use the No Action rule, it is up to you to ensure that the consistency of the object graph is maintained. You are responsible for setting any inverse relationship to a meaningful value. This may be of benefit in a situation where you have a to-many relationship and there may be a large number of objects at the destination.

So I feel like I should be using Nullify instead (which I am currently using), but the documentation for Nullify says:

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.

Now, as written, this seems to imply that if you delete a Profile, every Looking For that was associated with that Profile will have its relationship with Profile emptied, i.e. be removed from all Profiles.

What is the correct thing to do in this case?

Upvotes: 3

Views: 1990

Answers (1)

Martin R
Martin R

Reputation: 539765

"Nullify" is correct. Assume that the Profile p1 is related to the Looking Fors l1, l2, l3. If p1 is deleted and the relationship is set to Nullify, then only p1 will be removed from the inverse relationships in l1, l2, l3.

With "No Action", the inverse relationships in l1, l2, l3 would remain unchanged, and therefore point to a non-existing element p1. You would have to remove p1 "manually" from those objects.

In other words, "Nullify" is the simplest rule that keeps the object graph consistent: If a is related to b, and b is deleted, then b is not related to a anymore.

Upvotes: 6

Related Questions