Purged document (access removed) does not trigger QueryChangeFlow

Hallo,
I am using queryChangeFlow() on my Query to keep the documents updated inside a RecyclerView. When documents get updated/deleted and pulled to the device, everything works fine. But when removing the channel of a document, the replicator gets the document with an “ACCESS_REMOVED” flag and purges it from the lite database. But the queryChangeFlow() does not trigger on the removed document change.
Only on a new call of the Flow function (getPublicConsumptionListFlow) the documents results are refrehed correctly.

I thing it is a bug of the Couchbase queryChangeFlow() Function?
Is it somehow possible to refresh the flow when receiving the document over the documentReplicatorFlow?

Query Flow:

override fun getPublicConsumptionListFlow(): Flow<List<ConsumptionModelDTO>> {
        val db = databaseManager.getConsumptionDatabase()
        val query = db?.let { DataSource.database(it) }?.let {
            QueryBuilder.select(
                SelectResult.expression(Meta.id),
                SelectResult.all())
                .from(it.`as`("item"))
                .where(Expression.property("type").equalTo(Expression.string("consumption")))
        }

        val flow = query!!
            .queryChangeFlow()
            .map { qc -> mapQueryChangeToConsumptionList(qc) }
            .flowOn(Dispatchers.IO)

        query.execute()
        return flow
    }

Replicator Config:

        replicator =
            Replicator(
                ReplicatorConfigurationFactory.create(
                    database = evDatabase,
                    target = URLEndpoint(url!!),
                    type = ReplicatorType.PUSH_AND_PULL,
                    continuous = true,
                    authenticator = SessionAuthenticator(session),
                    enableAutoPurge = true
                    )
                )

Found a workaround, it’s not very impressiv and has some overhead, but it’s doing the job for now:
With a documentReplicationFlow on the replicator I catch when a document is assigned with the “ACCESS_REMOVED” flag (which will be purged automaticly from the local database). Then I add and remove a empty document to the database to trigger the queryChangeFlow.

    private suspend fun purgeWorkaround() {
        val repl = databaseManager.getReplicator()
        val replicatedDocs = repl!!.documentReplicationFlow()
            .map { update -> update.documents }
            .flowOn(Dispatchers.IO)

        replicatedDocs
            .collect { list ->
                val purgeBool = list.any { it.flags.contains(DocumentFlag.ACCESS_REMOVED) }
                if (purgeBool){
                    saveConsumption(Consumption(null, null, null, null, null,null,null,null,true), "purgeWorkaround").collect()
                    deleteConsumption("purgeWorkaround").collect()
                }
            }
    }

Thanks for this, @TobiP00! I’ve created CBL-3122 to track the issue.

2 Likes