Search code examples
androidcouchdbcouchbase-litecouchbase-sync-gateway

Android Couchbase Lite Replication


I am having issues getting the replication function to work for my local Couchbase database and my Android app:

private void startSync() {
    URL syncUrl;
    try {
        syncUrl = new URL("http://10.0.2.2:4984/sync_gateway"); // I am testing with the Android emulator

        manager = new Manager(new AndroidContext(this), Manager.DEFAULT_OPTIONS);
        database = manager.getDatabase("db");

        Replication pullReplication = database
                .createPullReplication(syncUrl);
        pullReplication.setContinuous(true);
        pullReplication.addChangeListener(this);
        pullReplication.start();

    }
    catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    catch (CouchbaseLiteException e) {
        e.printStackTrace();
    }

    Query query = database.createAllDocumentsQuery();
    query.setAllDocsMode(Query.AllDocsMode.ALL_DOCS);
    QueryEnumerator result;
    try {
        result = query.run();

        for (Iterator<QueryRow> it = result; it.hasNext(); ) {
            QueryRow row = it.next();
            Log.i("CouchActivity", "Getting document i: " + row.getDocumentId());
        }
    }
    catch (CouchbaseLiteException e) {
        e.printStackTrace();
    }
}

Once I have created a pull replication, I proceed to query all the documents for the local database, but no documents are returned.

My config file for the sync gateway is as follows:

{
  "interface": ":4984",
  "adminInterface": ":4985",
  "log": ["REST"],
  "databases": {
    "sync_gateway": {
    "server": "http://localhost:8091",
    "bucket": "stations",
    "sync": `function(doc) {channel(doc.channels);}`,
    "users": { "GUEST": { "disabled": false, "admin_channels": ["*"] } }
    }
  }
}

When I type in localhost:4984/sync_gateway, I do get a response of "

{"committed_update_seq":1,"compact_running":false,"db_name":"sync_gateway","disk_format_version":0,"instance_start_time":1471324911376777,"purge_seq":0,"state":"Online","update_seq":1}" 

Not sure if the issue is with the Android side, as I do see the sync gateway output "POST /sync_gateway/_changes" when I run my Android code. Can anyone explain why replication is not working?

Update - I was able to confirm that my setup was done correctly. The issue that I was having had to do with the way I created my documents. The ones that I created through the admin console did not have the metadata that is required in order to be recognized as a valid document. I ended up populating the server side database by populating the database through my app via push replication. Creating a document via the POST request should also work.


Solution

  • If you are adding documents through the Admin Console, you are missing the Sync Gateway in between. The tracking of changes for a particular Document(it's sequence number) is maintained at the Sync Gateway. So when an Agent is trying to pull a Document it finds no changes in reference to its local DB and hence you receive nothing. To actually pull a document you have to first Push it from your agent through the Sync Gateway. Admin Console directly adds documents to the SQL tables at server which has no co-relation to Sync gateway.