Overwrite Couchbase documents on Android

Recently I’ve been Woking on a project using Couchbase Lite on Android and I had an issue overwriting the tables in database (they call the tables “documents” in Couchbase) by following the code from the official guide. Therefore, I’m writing this post to jot down and share the solution I’ve figured out to overwrite the documents.

So here’s the code in the “Updating documents” section on the official guide.

Document doc = database.getDocument(myDocID);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("title", title);
properties.put("notes", notes);
try {
} catch (CouchbaseLiteException e) {

According to the official guide, the code above replaces the document’s body. In theory the code doesn’t have any problem, but there will be update conflicts when you execute the code on a document that its’ current revision has been read. Even your code is single thread and you think there isn’t anywhere else accessing the document, there could be conflicts for still as the replicator in Couchbase Lite library runs on the background thread. If you want to know more about this, I recommend reading the official document and the official blog post – A better way to update with Couchbase Lite.

Since the method pustProperties doesn’t handle the error like the other method update, it simply throws out a CouchbaseLiteException with HTTP status 409. Therefore, the document cannot be overwritten by using the code from the official guide.

Below is the solution I’ve figured out to overwrite the document’s body.

private void overwriteDocument(Document doc, Map<String, Object> properties) {
    UnsavedRevision mUnsavedRev = doc.createRevision();

    try {
    } catch (CouchbaseLiteException ex) {

As I have several documents to overwrite, I created a method to handle the overwrite process for me.

What the method does here is to obtain a new UnsavedRevision from the document by using createRevision(), then set the document contents into the newly created revision, and save the revision to the document. Since I didn’t save the copy of document contents from the current revision using getProperties, the document’s body is replaced by the Map received. In this way, I can overwrite the documents without conflicts.

I haven’t found any issue using this method in my project so far. If you have any problem with my solution, feel free to levae a comment below. I hope you find this post useful, and I’ll see you in the next post. Cheers~

2 Replies to “Overwrite Couchbase documents on Android”

  1. Nice article. I’m wondering, though, why you don’t use the DocumentUpdater approach? (It’s discussed at the end of the blog post you referenced.) It works somewhat like your solution, but with the extra feature of automatically detecting conflicts.

    1. What a surprise that you came over and leave a comment.

      Tbh I didn’t notice that I can overwrite the documents by using DocumentUpdater. This solution is kind of a product from a rush development, so I must admit that I missed quite a few things while reading the documents.

      My original thought was I’m overwriting the documents and my application doesn’t need to care about the old document’s body. Therefore, when I found the error message told me the documents failed to overwrite due to conflict, I simply just go for the way that let the documents always take the new revision.

      I’ll include a new method using DocumentUpdater in my post later. Thanks for you advise =]

Leave a Reply