我在使复制功能适用于本地Couchbase数据库和Android应用程序时遇到问题:

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();
    }
}


创建请求复制后,将继续查询本地数据库的所有文档,但不会返回任何文档。

我的同步网关配置文件如下:

{
  "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": ["*"] } }
    }
  }
}


当我键入localhost:4984 / sync_gateway时,确实收到“

{"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}"


不确定问题是否出在Android方面,因为我在运行Android代码时确实看到同步网关输出“ POST / sync_gateway / _changes”。谁能解释为什么复制不起作用?

更新-我能够确认我的设置正确完成。我所遇到的问题与我创建文档的方式有关。我通过管理控制台创建的文档没有要被识别为有效文档所需的元数据。我最终通过推复制通过我的应用程序填充数据库来填充服务器端数据库。通过POST请求创建文档也应该起作用。

最佳答案

如果要通过管理控制台添加文档,则两者之间将缺少同步网关。在Sync Gateway上维护对特定文档(其序列号)的更改的跟踪。因此,当代理尝试提取文档时,它不会发现对其本地数据库的引用发生任何更改,因此您什么也收不到。
要实际提取文档,您必须首先通过Sync Gateway从您的代理推送文档。管理控制台直接将文档添加到服务器上的SQL表中,而该表与Sync网关没有关联。

09-30 14:45
查看更多