简体   繁体   中英

Core Data - nullify delete rule

I have a many-to-many relationship between the entities Groups and Contacts. One group can have many contacts, and one contact can exist in many groups.

Now If I were to delete a group how should I handle all the references to the contacts that it holds?

As I see it there are two options: to use the nullify rule, that would set the deleted group reference to nil for those contacts that had that group. But this sort of introduces a micro-management problem. I then have to check each time I'm listing a contacts groups if any of the groups are nil.

So for me the more elegant solution would be that when a group is deleted, I would delete the group reference completely(?) from the contact object so that it will not contain any nil values for groups at all. Is that possible?

But this sort of introduces a micro-management problem. I then have to check each time I'm listing a contacts groups if any of the groups are nil.

No. See this answer to a similar question . If you set the deletion rule for both the Groups->>Contacts and Contacts->>Groups relationships to nullify, then:

  • deleting a contact will remove that contact from any groups that include it

  • deleting a group will remove that group from any contacts that belong to it

Collection objects in the Foundation framework (NSArray, NSSet, etc.) never have "nil values." It is possible to store the NSNull singleton as a value in a collection, but that's not what the nullify deletion rule does.

It sounds like the nullify rule is appropriate for both relationships in your situation. The only thing you need to be concerned about is what should happen when you delete all the contacts that belong to a given group or vice versa. A contact that doesn't belong to any groups makes sense, but what should happen to a group that contains no contacts? If a group with no members doesn't make any sense for your app, you'll have to deal with that case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM