How to get only changed documents by LiveQuery


#1

When something changed in database, LiveQuery just notifies about it. Why doesn’t it return exactly what has been changed? If I add a log message in a view, I see only changed document, but I can’t get the document right from the view.

Is there any simple way using couch api get only changed documents? Or everybody does it by themselves?


#2

Hi @PavelVPV,

Yes this currently a limitation of the live query feature.

Which platform are you developing for?

You can read this tutorial (for Android) that explains how to use the database change listener instead of a live query to drive animations in a recycler view https://github.com/couchbaselabs/mini-hacks/blob/master/android-recycler-view-animations/README.md.

Let me know a bit more about your use case and I’ll try to help.

James


#3

Hi @jamiltz,

I’m developing for Android.

I have 2 databases and I have 2 Android Activities so for each Activity I need to obtain different kind of documents from both of the databases. I have predefined in twos Views and twos LiveQueries dedicated to each Activity. Since only one of Activity can work at the same time (I don’t need to receive changes for inactive Activity), I stop LiveQueries dedicated to the inactive Activity.

RecyclerView is exactly what I’m using in both Activities.

Also while the one of Activities always requests one type of documents, the other one may requests the different type of documents.

The problem happens when some changes happen in the databases. I have some kind of adapter class for each document. If I have hundreds of documents, I have to recreate this adapter for each document.

That’s why I’m interested in the changes only. Of course, I can make some optimizations with the adapter class to reuse the oldest objects. Also I tried to find only changed documents from LiveQueries by myself, but for me it looks like my application and Couch have the same copies of documents, so this will cause a redundant memory usage.


Get notified when a document changes in a view / Is it safe to use "_local_seq"?
#4

As explained in the tutorial, you can use the database change listener to be notified of changes.
http://developer.couchbase.com/documentation/mobile/1.1.0/develop/guides/couchbase-lite/native-api/database/index.html#database-notifications

There’s a boolean property on the Database.ChangeEvent called isExternal. You could use that property to be notified of the changes and add those changes only to the RecyclerView.

The other option is to do a diff between the previous and new result enumerator when the live query triggers a LiveQuery.ChangeEvent.

James