Couchbase Lite Background Work

Hi everyone. I am currently using CouchBase Lite, CouchBase Server and Sync Gateway as part of an Android App. Now that I wrote down a basic scheme of what I would like to do (and I have already started coding something, but it is not Couch related, just Models and stuff), I wanted to move database operations in the app background, mostly for UI purposes. Now, I would like to know what is the best approach to achieve this. I googled something, both database generic and couchbase specific, but the results were confusing and contradictory. I resorted to search on this forum, but I could not find topics of help. Maybe I missed some of them. If so, could anyone link them? Or, if anyone could point a nice way of moving database work in the background, that would be great. As of now, I read about ASyncTasks, Workers, Loaders, Handlers. What would fit best CouchBase Lite operations? Thanks in advance!

What version of Couchbase Lite?

It sounds like you’re not experienced with threading / concurrency … if so it would be best to start by learning about the topic in general, as it’s one of the hardest things to get right in programming. There are a number of books about Java threading, and any good Android book should cover the topic too.

Hi, and thank you for your answer. The version of Couchbase Lite I am using is 2.0. As regards concurrency, I already worked and I am currently working with it. I might have explained myself in the wrong way, since I am not English. I understand what concurrency is, I am a student, I studied concurrency and threading. What I really wanted to know is specific to Couchbase. Since Android Developer site provides different guides on structuring threads and threaded work, I was just wondering if anyone with more experience than me could give a hint or two about best practices in threading for Background Couchbase Lite work. With an example, I hope to make it clear. I am running my app, and I decide to test it with a provisional user. I send a REST request, be it on a separate backend app server or directly on Sync Gateway (_session). When the login successfully returns, if set correctly Sync Gateway should open a channel for this user related documents to pass through. Now, when Couchbase acquires such documents, I also need to query for this documents on my app to retrieve data. Since UI Thread needs not to be overloaded or stopped, I must do this kind of work in the background, on another thread. Now, I am wondering, what is the best approach? Executor of a Runnable on a separate Thread? Callbacks?
I am sorry for the wall post, and for my English. Thanks in advance!

CBL 2.0’s API is thread-safe. If you need to perform something asynchronously, such as a query, just run it on a background thread and pass the results back to the main thread.

I send a REST request, be it on a separate backend app server or directly on Sync Gateway (_session). When the login successfully returns, if set correctly Sync Gateway should open a channel for this user related documents to pass through.

I’m confused by this. You don’t need to do any of that; just use the Replicator class to start a pull replication.

Now, when Couchbase acquires such documents, I also need to query for this documents on my app to retrieve data.

Use a database change listener to get called when a document is modified; then you’ll be called when the replicator pulls docs and can respond appropriately.

Now, I am wondering, what is the best approach? Executor of a Runnable on a separate Thread? Callbacks?

It’s been years since I used Java, and I’ve never developed for Android, so I can’t answer in detail. But if you’re familiar with how concurrency works in that environment, then it should be straightforward.

But really, what I would advise is to build the app without premature optimization. Then when it’s working, test its performance under load and look at how to use concurrency to speed up areas that are slow.