couchbase-lite-java-ce-root icon indicating copy to clipboard operation
couchbase-lite-java-ce-root copied to clipboard

QueryChangeListener is not called when purging a document

Open dlevache opened this issue 6 months ago • 0 comments

On Android, I encounter an incorrect behaviour on the query listeners.

Deleting and purging an existing document

When deleting and purging (or directly purging) a document from a collection, some query listeners are not called. My model is partly similar to that, and I store multiple items with the same base structure in a collection.

{
    "attributes": {
        "id": "lorem",
        "type": "type1"
    },
    "field-1": "...",
    "field-2": "...",
    "field-n": "..."
}

When I observe changes on a specific query, I don't get any notification if I delete and purge an entity in the same transaction.

In my code, I have:

database
    .createQuery("""SELECT * FROM `my-collection` WHERE attributes.type = "type1" LIMIT 1""")
    .queryChangeFlow()
    .mapToEntites()
    .map { entities -> entities.firstOrNull() }
    .collect { entity ->
        // Do something with the nullable entity
    }

And, in another place, I have this deletion code:

val idsToDelete = database.query("SELECT META.id() FROM `my-collection` WHERE ...").mapToIds()
database.inBatch<Exception> {
     itemsToDelete.forEach { documentId ->
        if (purge) {
            purge(documentId)        
        } else {
            val document = getDocument(id)
            delete(document)
        }
     }
}

The result is the same if I don't call delete(document) and call only purge(documentId). However, calling delete(document) and purge(documentId) in separate batch triggers the notification in the query.

That behaviour is not observed when the listener is registered on a document:

collection
    .documentChangeFlow(documentId)
    .collect {
        // Also triggered when deleting / purging
    }

EDIT: It seems to be working better when calling delete and purge sequentially. My first version was calling purge only since it also deleted the document.

Purging an already deleted document

The same incorrect behaviour can be observed when a listener is registered on the deleted items ("SELECT META().id FROM ... WHERE META().deleted"). The listener is not called when any of the deleted documents is purged.

 

By observing those two behaviours, I suppose there is a missing trigger emission when purge is called. Can you confirm this, please?

dlevache avatar Apr 16 '25 12:04 dlevache