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. I have implemented a method "didDelete" on
my Person class and here is the block that ensures that a person's note
documents get deleted if their owning person gets deleted:
[_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];
}
}];
For some reason, newRevision.properties was always null, hence I'm using
newRevision.document.
On Sunday, December 22, 2013 9:41:04 PM UTC+1, Pascal wrote:
>
> I'm not sure how to handle deletes properly in this scenario:
>
> I have a "Person" model that may have a "Note" model as a property. Works
> fine, in Person's `save:` I check for the note property as well and save it
> when needed. I did the same in `deleteDocument:` of Person, also delete the
> note if one exists. However, when deleting Person objects from a table view
> with Couchbase Lite's nice table view source, `deleteDocument:` doesn't get
> called and the person's note lives on a lonely live in the database.
>
> What's the best way to handle such dependencies, should I use a validator
> and delete the note in that block when the person gets a tombstone? Since I
> intend to have another property on Person objects that will be an array of
> models this question also extends to the case of one-to-many relationships.
>
--
You received this message because you are subscribed to the Google Groups
"Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/mobile-couchbase/9ff59970-7298-4f42-937a-77c9012479ad%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.