cloudant / sync-android

A JSON-based document datastore for Android applications
Apache License 2.0
267 stars 91 forks source link

Replications when app is in background #590

Closed acutetech closed 6 years ago

acutetech commented 6 years ago

I am using Android Beacon Library to detect Bluetooth devices. This library has given much thought to how to detect BLE devices while the app sleeps, for different Android versions, and it appears to do this very well: my app sees the beacon quickly, regardless of the state of the app or device.

My problem is what happens next: I am using sync-android library to store BLE events and sync with a remote database. When the app is active the replication happens immediately, but when the phone has been idle for a time the replication takes a long time: 10s of minutes.

The documentation for Replicator.start() says "start will spawn a manager thread for the replication and immediately return." So perhaps the long latency is because Android OS is not scheduling that thread to run? Or could it be with the network parts of the OS? I'm testing with Android 7.

Any insights into what might be going on, and strategies? I have looked at replication-policies.md - would it help to set up a JobScheduler to do the replication? Or will I hit the same problem? Can I do anything to affect the priority of the Replicator.start() thread? (I am trying to do ad-hoc push replications, rather than anything periodic). Note the Android 8 restrictions described in the link above.

Thx - Charles

smithsz commented 6 years ago

Hi @acutetech, We'd advise using the JobScheduler. It's intelligent about scheduling the background work, considering memory pressure and network state. You'll need to wrap the Replicator.start() call in a JobService. This enables the system to perform your work for you, regardless of whether your app is active.