Search code examples
iphonecore-datacontactsrelationships

How can I maintain a CoreData table which can be edited outside its relationship to another table?


Let's say I have a table called "Event" which has a to-many relationship with table "Group" which has a to-many relationship with table "Person".

Many groups can be created which could reference the same person.

I have not added an inverse relationship yet from "Person" to "Group" since if a "Group" is deleted I do not want the associated "Person" to be deleted as it may be referenced somewhere else.

Should I just leave out the inverse relationship? The docs advise strongly against this but I think this many be an instance where it is the right way to go.

Any help is appreciated.


Solution

  • Unless you have a very good reason to do otherwise, you should always have an inverse relationship. It helps CoreData maintain the integrity of the object graph. The default delete rule for a relationship is "Nullify", which sounds like what you want. "Nullify" means that if you delete this object, the relationship attribute on the other end of this relationship gets nilled-out (for a to-one relationship) or has the value removed from the set (for a to-many relationship). The only way for the other end of the relationship to actually be deleted is if you change the delete rule to specify that.

    tl;dr: Use inverse relationships, leave the Delete Rule at the default of "Nullify".