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/com.couchbase.toshiba.testcouch D/couchbaseevents: New revision added: {Drinks #31-6e157d8dd87918e5b2140a1d0ac56a87 @35}. Conflict: false
08-23 03:48:43.324 5867-5989/com.couchbase.toshiba.testcouch E/Database: Unknown Exception: com.couchbase.lite.Database[/data/user/0/com.couchbase.toshiba.testcouch/files/testdata.cblite2] got exception posting change notifications
java.lang.RuntimeException: Can’t create handler inside thread that has not called Looper.prepare()
at android.os.Handler.(Handler.java:200)
at android.os.Handler.(Handler.java:114)
at android.widget.Toast$TN.(Toast.java:345)
at android.widget.Toast.(Toast.java:101)
at android.widget.Toast.makeText(Toast.java:259)
at com.couchbase.toshiba.testcouch.MainActivity$5$1.changed(MainActivity.java:125)
at com.couchbase.lite.Document.revisionAdded(Document.java:501)
at com.couchbase.lite.Database.postChangeNotifications(Database.java:2222)
at com.couchbase.lite.Database.storageExitedTransaction(Database.java:696)
at com.couchbase.lite.store.SQLiteStore.endTransaction(SQLiteStore.java:2171)
at com.couchbase.lite.store.SQLiteStore.runInTransaction(SQLiteStore.java:690)
at com.couchbase.lite.replicator.PullerInternal.insertDownloads(PullerInternal.java:548)
at com.couchbase.lite.replicator.PullerInternal$2.process(PullerInternal.java:117)
at com.couchbase.lite.support.Batcher.processNow(Batcher.java:383)
at com.couchbase.lite.support.Batcher.access$300(Batcher.java:16)
at com.couchbase.lite.support.Batcher$2.run(Batcher.java:317)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
08-23 03:48:48.353 5867-13910/com.couchbase.toshiba.testcouch E/RemoteRequest: Got error status: 409 for http://192.168.1.130:4984/testdata/_local/ff3fed2c75f2ef09832217300ae8867dda7d6627. 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() {
@Override
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();

                    documentChanged.countDown();
                }
            });
            try {
                doc.createRevision().save();
            } catch (CouchbaseLiteException e) {
                e.printStackTrace();
            }
        }

Looks like you’re not posting the toast on the UI thread. Check out this Stack Overflow post: http://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare

1 Like

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

Thanks so much for your help :slight_smile: