I am working on a couchbase lite driven app and trying to do live query based on this help from couchbase mobile lite.
While it works, I am confused on the number of documents that reported as changed. This is only in my laptop so I uploaded json file to couchbase server via cbimport. Then sync gateway did sync all the data succesfully to my android app.
Now, I changed one document in couchbase server but all 27 documents are returned as changed in the live query. I was expecting only the document I have changed to be returned as changed since the last sync time.
Looking at the meta information of each document, the document I have changed have the following:
{
"meta": {
"id": "Group_2404_159_5053",
"rev": "15-16148876737400000000000002000006",
"expiration": 0,
"flags": 33554438,
"type": "json"
},
"xattrs": {
"_sync": {
"rev": "7-ad618346393fa2490359555e9c889876",
"sequence": 2951,
"recent_sequences": [
2910,
2946,
2947,
2948,
2949,
2950,
2951
],
"history": {
"revs": [
"3-89bb125a9bb1f5e8108a6570ffb31821",
"4-71480618242841447402418fa1831968",
"5-4c4d990af34fa3f53237c3faafa85843",
"1-4fbb4708f69d8a6cda4f9c38a1aa9570",
"6-f43462023f82a12170f31aed879aecb2",
"7-ad618346393fa2490359555e9c889876",
"2-cf80ca212a3279e4fc01ef6ab6084bc9"
],
"parents": [
6,
0,
1,
-1,
2,
4,
3
],
"channels": [
null,
null,
null,
null,
null,
null,
null
]
},
"cas": "0x0000747376881416",
"value_crc32c": "0x8c664755",
"time_saved": "2020-06-01T14:23:30.669338-07:00"
}
}
}
while the rest 26 documents are similar to this one:
{
"meta": {
"id": "Group_2404_159_5087",
"rev": "2-161344efd90c00000000000002000006",
"expiration": 0,
"flags": 33554438,
"type": "json"
},
"xattrs": {
"_sync": {
"rev": "1-577011ccb4ce61c69507ba44985ca038",
"sequence": 2934,
"recent_sequences": [
2934
],
"history": {
"revs": [
"1-577011ccb4ce61c69507ba44985ca038"
],
"parents": [
-1
],
"channels": [
null
]
},
"cas": "0x00000cd9ef441316",
"value_crc32c": "0xc37bb792",
"time_saved": "2020-05-28T11:34:50.3200745-07:00"
}
}
}
Is that the expected behavior or there is something I can do about it?
That behavior is as expected. The live query re-runs the query every time there is a database change that impacts the results of the query. So in your case, since it's a query that fetches ALL documents in your database, the query re-runs when any document in database changes and it returns all documents (which is what the query is for).
Live queries are best suited if you have a filter predicate on your query. For instance, if the app wants to be notified if the status field in documents of type "foo" changes. in that case, you will only be notified if the status field changes in document of type "foo".
In your case, if you just care about changes if any of the document in your database changes, you should just use a Database Change Listener