Document ChangeListener

Hi Guys, i am just tested something and it does not seem to work accordingly to plan.

Now i have 2 android phones, Phone A and Phone B, both are connected to the couchbase server through sync gateway.

Phone A Created Document ID 1 Rev 2-33
Phone A Push to Couchbase Sever
Phone B pulls from couchbase Sever
Phone B updates Document ID 1 Rev 3-22
Phone B Push to Couchbase Sever.

How can i notify Phone A that there is a change in document ? Or is it because i SetContinous to true it will forever listen to any document change in the sever and automatically sycn down.

I tried to use the Doc.addChangeListener example but it only triggers when i update a document on that particulate phone and not the other phone

As long as you are subscribed to the proper channels and have access to the document from both phones, you should receive a change when a new revision is created. Set continuous replication means that the device will “listen” forever for changes. Do you see any errors in the sync gateway or couchbase lite logs?

1 Like

Hey bro, i found the error already. Could you help understand the error?

This is my couchbase lite log:

08-23 03:48:43.323 5867-5989/ D/couchbaseevents: New revision added: {Drinks #31-6e157d8dd87918e5b2140a1d0ac56a87 @35}. Conflict: false
08-23 03:48:43.324 5867-5989/ E/Database: Unknown Exception: com.couchbase.lite.Database[/data/user/0/] got exception posting change notifications
java.lang.RuntimeException: Can’t create handler inside thread that has not called Looper.prepare()
at android.os.Handler.(
at android.os.Handler.(
at android.widget.Toast$TN.(
at android.widget.Toast.(
at android.widget.Toast.makeText(
at com.couchbase.lite.Document.revisionAdded(
at com.couchbase.lite.Database.postChangeNotifications(
at com.couchbase.lite.Database.storageExitedTransaction(
at com.couchbase.lite.replicator.PullerInternal.insertDownloads(
at com.couchbase.lite.replicator.PullerInternal$2.process(
at java.util.concurrent.Executors$
at java.util.concurrent.ScheduledThreadPoolExecutor$
at java.util.concurrent.ThreadPoolExecutor.runWorker(
at java.util.concurrent.ThreadPoolExecutor$
08-23 03:48:48.353 5867-13910/ E/RemoteRequest: Got error status: 409 for Reason: Conflict

And this is my code in android studio

Document doc = database.getDocument(“Drinks”);
final CountDownLatch documentChanged = new CountDownLatch(1);
doc.addChangeListener(new Document.ChangeListener() {
public void changed(Document.ChangeEvent event) {
DocumentChange docChange = event.getChange();
String msg = “New revision added: %s. Conflict: %s”;
msg = String.format(msg, docChange.getAddedRevision(), docChange.isConflict());
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();

            try {
            } catch (CouchbaseLiteException e) {

Looks like you’re not posting the toast on the UI thread. Check out this Stack Overflow post:

1 Like

bro, thanks for your answer! It worked! thank you so much!

Thanks so much for your help :slight_smile: