Search code examples
iphoneios4core-datacascading-deletes

How to define conditional cascade delete rule in Core Data?


My object graph contains two entities: Author and Book with one to many relationship (one author may write many books)

I wish that when a book is deleted, the author will be deleted as well but only if there are no other books in the database connected to that author. (that means that author should be deleted only when the last author's book is deleted)

What is the best way to do this?


Solution

  • You can put your deletion logic in your -prepareForDeletion method on your NSManagedObject. You should be able to assert any policy you want then.