Live Query blocking UI thread


#1

Hi,

We are currently using Couchbase 2.1.2 in our Android application.
We have around 70k documents in our app.

We have 5 live queries running in our app.

We know that whenever there will be db change notification, the query runs again, it compares previous results to the new results, and if there is a difference in the result set, it gives a notification.

What’s currently happening is that whenever we are getting frequent changes from our server, the UI thread of the application seems to get blocked. We are not currently passing the optional Executor parameter while adding the QueryChangeListener.

We wanted to confirm that the above process of comparing previous results to the new one, will that take place on the UI thread as well. We looked at the source code and it looks like it uses a SingleThreadScheduledExecutor to compare the previous results to the new one. We wanted to confirm if that was right, and if it is can you please suggest what could be any other process that is blocking the UI thread.

Thanks


#2

Couchbase Lite 2’s query listener implementation runs the queries on the database’s default thread. If that’s the UI thread, it can degrade responsiveness. Workaround is to use a db instance that’s associated with a background thread.


#3

Hi Jens,

Thanks for the help!

We have checked the code and found that queries are somehow running on default thread as you said ( Main Thread).
if we associate the DB instance with a background thread how will it be possible that queries run in that thread instead of the main thread(as default thread is Main Thread)?

Please find below code flow for query change listener -


adding query change listener calls start() method.


Inside this, query chage listener is called without executor as dbListnerToken is null.


If executor is null(changelistener without executor) then it will use Default executor.


and DefaultExecutor uses Main thread looper here. that’s why query change listener always runs on UI thread.

What would you suggest to do? If you can help us use couchbase-lite sdk as an android module then we can fix this and proceed with our further development.

Can we change default executor to single thread executor and use it.

Thanks!


#4

I’m not familiar with the Java implementation. @blake.meike?


#5

Yes. Exactly. Use a different executor. You might consider AsyncTask.THREAD_POOL_EXECUTOR