Best practices for Android

I’ve been fiddling with CB lite for a while now and I’m starting to encounter a few problems as a result of not approaching it from the right angle. I basically have a couple of activities that use the database and I haven’t been able to handle it well. What I did at first was to create a new instance of the StorageManager (which is the class that provides the database) on both activities and, since the database would be the same, I thought this would be ok for synchronization. This has created a lot of problems (specially when restarting the Replication pusher every time I resume an activity) and I thought it would be better to look it up here in the forum.

I’ve seen some recommendations about using a singleton or a content provider, as well as having a service running in the background doing all the syncing.

Using a simpleton is not recommended in android unless you use a library that manages the threads, and making a content provider seems like overkill to me.

Is there a sample case for this that I haven’t come by yet? am I missing something?

Thanks in advance.

2 posts were split to a new topic: Single Process Access to DB - Best Android Practices

Hi @priya.rajagopal,

Is it alright to use the same CBL in 2 applications simultaneously, Will Database change listeners and live queries work if both applications are using the same CBL?

Why I am asking this as we are looking to create a back ground service which will keep the device in sync with the server even if the App is not active. But then will App get all the change listener events if the data is synced by background service?

Regards
Pankaj Sharma

The database can be accessed from multiple processes, but notifications and LiveQueries won’t trigger if a change was made by another process.

We hope to fix this, since it can be a useful design, but haven’t gotten to it yet.

If you need the app to receive updates at any time, it’s better for the device’s battery life if you use push notifications to wake up the app, instead of having a process checking the server all the time.

I noticed this immediately, is there a workaround you guys use in these cases? Maybe using RxJava to observe the LiveQuery in the correct thread?

I am a newbie with RxJava so that might have been a really stupid thing to say.

@jens The issue is if the app stops for some period of time it goes for _rev_diff calls. and in my case it is 480 calls to sync gateway. That blocks other data which I need to send. Hence looking for a background app.

What I am doing is populating CBL with syncing it in a java app and then provide this CBL as zip to app when they launch for the first time. is it the reason for _rev_diff calls? If yes then how can I make sure that these calls are done for the first time and not again if all data is already there (last sequence number).

Please help.

Regards
Pankaj Sharma

If you need to use the Android service , then you would have to leverage Android’s notification system/ intent service for notifying your app about availability of data.

4 posts were split to a new topic: Accessing Couchbase Lite via a Content Provider in Android

We’ve got several topics under one thread here at this point. I’m splitting them into a few separate subjects.

Unfortunately Discourse seems to require keeping the first post when splitting out replies and creating a new topic. You may need to ignore and read past the first post for things to make sense.

@jens is talking about different processes, not different threads. RxJava is for handling async within a process, so it doesn’t apply.

RxJava can be a nice way to ensure your database work is not on the main thread in Android.