[_database setValidationNamed:@"personNoteValidation"
asBlock:^(CBLRevision *newRevision, id<CBLValidationContext> context) {
// delete notes on person deletion
if (newRevision.isDeletion && [@"person" isEqualToString:newRevision.document.properties[@"type"]]) {
PersonClass *person = [PersonClass modelForDocument:newRevision.document];
[person didDelete];
}
}];
To answer my own question, maybe this is useful to somebody else: it seems handling deletion of dependent documents (for example a one-to-many relationship between people and note documents belonging to somebody) is best done in a validation block.
For some reason, newRevision.properties was always null, hence I'm using newRevision.document.
I’m a nervous about the idea of a validation block making changes to the database, for instance if your -didDelete method ends up deleting other documents. It will trigger a re-entrant call to the document-updating code, and I’m not sure that will behave correctly in all cases.It would be safest if you delayed the call to -didDelete, e.g. by putting it in a dispatch_async block.
For some reason, newRevision.properties was always null, hence I'm using newRevision.document.Deleting a document creates a revision with no properties (except a magic “_deleted”:true tombstone.) If you want to look at the properties of the existing revision, look at oldRevision.properties.