Recommended approach to app suspend/resume when running CBL continuous replicator

The documentation seems to make no mention of what to do with a continuous Couchbase Lite replicator if the mobile app suspends/resumes. Will LiteCore/CBL deal with this internally – will it stop by itself, will it disconnect (cleanly) by itself? – or does the app need to make sure it stops the replicator on app suspend and restart the replicator on app resume? Or should a continuous replicator always be run as a background service, so it continues even if the app is not in the foreground?

I have been struggling with this so I was wondering what the recommended approach is for this.

Swift / Obj-C will manage this for you but for all other platforms you must manage this yourself.

So if your replicator is still running when your app is suspended, and the OS terminates it, then it’d be a sudden unclean disconnect?

I tried to clean up the replicator on a suspend event, but I am not sure if I succeeded, I need to spent some more time on it.

There would probably be a disconnect before that time on platforms that don’t manage this for you because the program would not be responding in the background (except maybe Android? I’m not familiar with how it works but I don’t think it suspends in the same way). If the replication is running (foreground or background) and the app is terminated then it is no different than as if the app suddenly crashed and the disconnect would be unclean.

Thanks for the information Jim.

Jim is correct. Leaving a continuous replication going in Android is a bad idea. I believe that there are two good strategies for managing replications on Android:

  1. in a foreground service: I believe that this is the only reasonable place to put a continuous replication.
  2. in a Work Manager task. Not continuous and managed by the app. This is much more Android-like and much easier on the battery.
1 Like