Do not sync a deleted document if it has never been synced before

Hi there!

I have a problem with filtering deleted documents on a push replication.

I have a very simple filter that prevents pushing documents to the sync gateway based on a property. In Swift:

private func setContactsFilter() {
    self.database.setFilterNamed(SyncManager.cloudContactsFilter) { rev, _ in
        guard let props = rev.properties as? [String: AnyObject] else {
            return true
        }
        return SyncManager.shouldSyncContact(props)
    }
}

class func shouldSyncContact(properties: [String: AnyObject]) -> Bool {
    if let type = properties["type"] as? String where type == "contact" {
        return false
    }

    return true
}

(The if is a bit more complex than that, but you get the idea.)

Now when I delete a document that was never synced, the filter function “fails” to prevent it from syncing, since the document does not have any properties anymore. The deleted document get synced to the database instead of just being forgotten.

I am not sure how to fix this.

  • Should I use purgeDocument instead of deleteDocument for those cases where I don’t want to deletion to be synced? This means that the code that deletes the document should be aware of the filter. I don’t like that
  • Should I use parentRevision in my filter when the revision is a deletion? Will it always work? What if the parent revision has been purged already?

Any ideas how to solve this?
Thanks

After some thoughts I am using

let properties = rev.isDeletion ? rev.parentRevision?.properties : rev.properties

But this means that for each deletion I will have a round-trip to the database to fetch the previous revision, right? This does not seem very efficient to me.