CBL Android and Thread Friendliness

79 views
Skip to first unread message

Ken Courville

unread,
May 5, 2015, 6:38:46 PM5/5/15
to mobile-c...@googlegroups.com
Howdy,

First, I'm having a hard time logging into forums.couchbase.com.  Keep receiving "Sorry, there was an error authorizing your account. Perhaps you did not approve authorization?"  Otherwise, I'd post there.

My question this time is: for Android CBL, should I be using one Manager per thread and one Database instance per database file per thread? 

All of the examples I've run across appear to run on the UI thread.  I'm confused on the documentation.  I see this: "Android, Java It is safe to call Couchbase Lite from multiple threads on the Android / Java platform. If you find any thread safety related issues, please report a bug."

Do you have an example using multiple threads and maybe multiple databases? 

Here's our scenario: 
Our applications are primarily Cordova-based.  Due to performance requirements and amount of data we're working with (100K+ documents), we're unable to effectively use JavaScript-based views.  Also, last I checked, JavaScript-based views are not compatible between Android and.NET CBL implementations.  Is this still the case?  One solution we're looking into is generating the initial data (which changes daily) and indexes on a beefy server using .NET CBL and then downloading the initial .cblite file directly to the device. 

For the moment, we're using Cordova plugins to wrap CBL-based processes. Some processes take a few seconds, so we have to run them on a background thread.  And, outside of the Cordova plugins, there are background processes to manage synchronization with the remote server that also interact with the CBL databases.

On the plus side, pulling from the CBL master branch has lowered our indexing times from 5 minutes per index to 30 seconds.  Thanks for that!

Hideki Itakura

unread,
May 6, 2015, 1:24:19 PM5/6/15
to mobile-c...@googlegroups.com
Hi Ken,

We recommends one Manager per an application. CBL supports multiple databases per a Manager.


> All of the examples I've run across appear to run on the UI thread.  I'm confused on the documentation.  I see this: "Android, Java It is safe to call Couchbase Lite from multiple threads on the Android / Java platform. If you find any thread safety related issues, please report a bug."

I believe it tries to say Database instance is thread safe. For example, pull replication is run in another thread beside main/UI thread, UI thread (add/update/delete document) and replicator thread (also add/update/delete document) can access same database.

> Also, last I checked, JavaScript-based views are not compatible between Android and.NET CBL implementations.

This is not because of JavaScript-based view. Current version of CBL Android and .NET has incompatible for views table. We need to address this.

Thanks!
Hideki 

Ken Courville

unread,
May 7, 2015, 10:11:45 AM5/7/15
to mobile-c...@googlegroups.com
Should it be save to have one Singleton instance of Manager and one singleton instance of Database-per-database file?

I'm battling this error at current:
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase:

--
You received this message because you are subscribed to a topic in the Google Groups "Couchbase Mobile" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mobile-couchbase/9ry5Cv5xkLg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/bc96e0d0-74b6-4f57-8bf8-3e34b15d6fce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ken Courville

unread,
May 7, 2015, 10:29:28 AM5/7/15
to mobile-c...@googlegroups.com
So far, it looks like using Singleton for Manager and just always calling "getDatabase()" works.  I suspect that Database expects to have one instance per thread, and the Manager takes care of that.

Regarding your comments on view compatibility, are you talking about JavaScript-based views?  I thought that .NET-based and Android-based views were compatible as of release 1.0.4.

Jens Alfke

unread,
May 7, 2015, 12:23:00 PM5/7/15
to mobile-c...@googlegroups.com

On May 7, 2015, at 7:29 AM, Ken Courville <krcou...@gmail.com> wrote:

So far, it looks like using Singleton for Manager and just always calling "getDatabase()" works.  I suspect that Database expects to have one instance per thread, and the Manager takes care of that.

Not exactly; each Manager will create one and only one Database instance for a physical database. In other words, getDatabase is idempotent (returns the same object every time you call it.) But if you created multiple Managers, each one would return a different Database object for the same database.

Under the hood there is only limited performance benefit to creating multiple Managers or Databases. You’ll get more parallelism for CPU-bound activity, but the SQLite database file has its own locks and (on iOS and Android at least) a write transaction blocks all other transactions, including reads, until it completes. (Part of the benefit of ForestDB is that it supports simultaneous reads and writes.)

—Jens
Reply all
Reply to author
Forward
0 new messages