Good morning everybody
I am facing this behavior and I do not understand if it’s the right one or there is something missing in the logic.
We have an android application with couchbase mobile lite and the syncgateway 2.7.3 (cb server 6.5.1)
- The application has a continuous replicator with a single channel “A”
- The application generates a document DOC where DOC.type = “B”
- on the sync gateway, we have a function like this one
function (doc, oldDoc) {
if(doc.type===‘A’) {
channel(‘A’);
} else {
return;
}
}
The point is to generate a document which immediately after the creation will be removed from the channel ‘A’
In fact, in the sync gateway logs I have a line like
2020-08-12T13:37:01.922+02:00 [INF] CRUD: Doc "ARCHIVE_b4cc1149-eaf8-40d1-b1cf-5ccdaac821bb" / "1-1d9fb34eb6d9d552e690394baaa466c337adcd9e" in channels "{}"
but the document still exists on the couchbase lite database, the replication is still configured to have documents only of the channel “A” so why is still there? Do I have to purge manually? When? The document must not exist but it’s not clear to me how to deal with this case.
Thanks for any help.
When a user loses access to a document as a result of the document being removed from all of user’s channels, then a subsequent pull of the document from the Sync Gateway would result in a document removal notification . This must trigger an auto-purge of the document in Couchbase Lite.
That said, based on my understanding of your workflow, the behavior is expected .
I see that your couchbase lite app is creating the doc and pushing it up …so there is no pull replication to trigger a removal notification . If you are looking to purge the document after pushing, see pattern2 in this blog
Note that channel access enforcement happens on read / pull replication.The assignment happens on a write
Thank you for your reply.
Pattern 2 is exactly what I was looking for.
In fact, I implemented this function in my code.
this.replicator.addDocumentReplicationListener(new DocumentReplicationListener() {
@Override
public void replication(@NonNull DocumentReplication replication) {
//implement sync gateway function
for (ReplicatedDocument d : replication.getDocuments()) {
Document doc = database.getDocument(d.getID());
if (doc != null) {
//re-implement sync-gateway function
String type = doc.getString("type");
if (!"A".equals(type)) {
try {
database.purge(d.getID());
} catch (CouchbaseLiteException e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
}
}
});
where I am basically re-implementing the sync-gateway function.
In additional I am applying the same pattern to the pull modification, so I have the same behavior on a device which is not the one that makes the change.
For example:
- I have N devices all listening to the channel “A” (same code on all devices) all with the document X
- One device (D1) change the X.type from “A” to “B”
- Having this code on pull and push I have seen the document X will be purged on D1 after push, and also from all the other devices after pull.
Thanks again to you and your team for the help.
1 Like