Reputation: 126127
I'm a little fuzzy on how the delete rules for relationships in Core Data work, at least beyond the simple cases described in the documentation.
Most of those cases, and most of the answers I've seen for questions here, use a model where the object on left side of a one-to-many relationship "owns" the objects on the right side: e.g. a Person
has PhoneNumber
s, and if you delete the person you delete all their associated numbers. In that kind of case, the solution is clear: Core Data will handle everything for you if you set the relationships like so:
Person --(cascade)-->> PhoneNumber
PhoneNumber --(nullify)--> Person
What I'm interested in is the opposite: A to-many relationship where the "ownership" is reversed. For example, I might extend the CoreDataBooks sample code to add an Author
entity for collecting all info about a unique author in one place. A Book
has one author, but an author has many books... but we don't care about authors for whom we don't list books. Thus, deleting an Author
whose books
relationship is non-empty should not be allowed, and deleting the last Book
referencing a particular Author
should delete that Author
.
I can imagine a couple of ways to do this manually... what I'm not sure of is:
Upvotes: 13
Views: 5019
Reputation: 257
The following worked for me:
Set the deletion rule on the 'book' relationship of your author entity to 'Deny' meaning that as long as there is a book linked to your author it cannot be deleted.
Subclass your book entity and override the prepareForDeletion() function as follows:
public override func prepareForDeletion() {
super.prepareForDeletion()
do {
try author.validateForDelete()
managedObjectContext?.delete(author)
} catch {}
}
Validate for delete will throw an error unless the book relationship is empty. You can optionally handle the error.
Upvotes: 1
Reputation: 649
Similarly to Tim's solution, you can override the willSave
method in your Author NSManagedObject
subclass. Note that if you do use Tim's solution, I highly recommend filtering the books set for books that haven't been deleted; this way if you delete all of the Author's books at the same time, the Author will still be deleted.
- (void)willSave {
if (!self.isDeleted) {
NSPredicate *notDeletedPredicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary<NSString *,id> *bindings) {
return ![(NSManagedObject *)evaluatedObject isDeleted];
}];
NSSet *filteredBooks = [self.books filteredSetUsingPredicate:notDeletedPredicate];
if (filteredBooks.count == 0)
[self.managedObjectContext deleteObject:self];
}
[super willSave];
}
Upvotes: 1
Reputation: 864
Rickstr,
Check below for the relationships to get your two criteria done.
Author -- (Deny) -->> Books
deleting an Author whose books relationship is non-empty should not be allowed
DENY: If there is at least one object at the relationship destination, then the source object cannot be deleted.
Book -- (Cascade)-- > Author
deleting the last Book referencing a particular Author should delete that Author
You cannot delete the Author, as our first rule is saying, if there are any Books which are non-empty should not be deleted. If they are not present the Author gets deleted.
I think theoretically it should work. Let me know, if this works or not.
Upvotes: 2
Reputation: 1682
You could override prepareForDeletion
in your Book
class and check if the author has any other books. If not you could delete the author.
- (void)prepareForDeletion {
Author *author = self.author;
if (author.books.count == 1) { // only the book itself
[self.managedObjectContext deleteObject:author];
}
}
Edit: To prevent deletion of an author with books you could override validateForDelete
or even better: don't call deleteObject with an author with books in the first place
Upvotes: 15