Preface:
I have two entities defined as a one-to-many relationship: A <<-------> B. B's relationship to A is called myAs and is a to-many relationship with Nullify as the Delete Rule. The inverse relationship from A to B is a to-one relationship with Cascade as the Delete Rule.
I have implemented validateForDelete on the B class like so:
- (BOOL)validateForDelete:(NSError **)error {
[super validateForDelete:error];
BOOL validDelete = FALSE;
if ([self.myAs count] == 0) {
validDelete = TRUE;
}
return validDelete;
}
The purpose of this is to delete the object B only if there are no more A objects in a relationship with it (but always delete the B object if there are no longer any A objects in a relationship with it). This validateForDelete works as intended if I check for this validation manually before a save: on a B object deletion:
if ([b validateForDelete:NULL]) {
//delete b object...
[context save:&error];
...
}
The problem I am having is when a delete of a B object is being cascaded from an A object deletion. Users will not have access to B objects directly--they are created and deleted via A objects. Therefore, my rule that B objects are to be deleted when there are no more A object associated with it must be enforced from the A object--hence the cascade on delete.
The problem is that when I delete an A object, the validateForDelete is called on the B object because of the cascade. I am getting an resolved error: Unresolved error (null), (null) because I am not calling the validateForDelete manually.
Question(s):
How do I access the validateForDelete call from a cascading delete programmatically so that I can pass in an error variable and/or handle a validateForDelete result of FALSE?
If the above is not possible, how should I be handling this use case? Is there another more practical way of achieving this?
Thanks in advance.