Android Replication stop doesn't work

I’m using couchbase lite on android and I crated continuous replication. I want to stop them as soon as my app goes to the backgorund or it’s killed. Here’s the code snippet how did I do that

@Override
    public void stopReplications() {
        if (push != null) {
            push.stop();
            Log.d("REPLICATIONSTATELOG", "push state " + push.isRunning() + "");
        }
        if (pull != null) {
            pull.stop();
            Log.d("REPLICATIONSTATELOG", "pull state " + pull.isRunning() + "");
        }
    }

Problem is that after push.stop() or pull.stop() pull.isRunning() or pull.isRunning() NOT ALWAYS return false. In most cases it will return true. Is this a bug or did I something wrong?

Which version of CBL are you running ?

Is may be possible that the replicator is wrapping up ongoing replication before stopping so few minutes before status change is reported.

Instead of just querying for status, can you register for replicator status change listener instead and see if it reports back stopped status eventually?

Replication is running on a background thread. You can think of the stop call as notifying the replication, but the code actually stopping it runs asynchronously. If you check status immediately, it will likely still show as running. If you need to know the status more reliable, you need to use a listener. I’ll edit this in a bit to include a link to a code example for doing this.

Short answer, this is the right way to stop replication, but the wrong way to verify it has stopped.

1 Like

What @hod.greeley said. Stopping a replicator is an asynchronous process. The replicator is not stopped until a listener receives a status change indicating that it is in the STOPPED state.