koen
koen

Reputation: 5729

Core Data: prevent deleting of shared entities

This is something about Core Data I quite don't understand yet.

Consider the following model:

Library <--> Book <<-->> Author

At one point the user decides to delete a book from a library, however the book shares an author with another book. What will happen with that shared author, how do I prevent it from being deleted from the store along with the to-be-deleted book?

Upvotes: 0

Views: 74

Answers (1)

Wain
Wain

Reputation: 119031

To begin with, ensure all of your relationships have inverses set (your little diagram has no book -> library).

Each end of a relationship has a delete rule which describes what happens when the other end of the relationship is deleted. This is what you should configure. The default is Nullify, which means that deleting a book will just remove that book from the list of books known by the author. The author will not be deleted.

Check the docs here for full details on the delete rules. You might want to consider, for example, setting the rule to Cascade on the library and the author such that if the library is deleted everything else is too (would require additional relationship from library to authors) and deleting an author will delete all of their books.

Upvotes: 1

Related Questions