Android - Couchbase lite - pull with filter - Replication.setFilter

时间:2017-11-10 10:21:25

标签: android couchdb database-replication pull couchbase-lite

我正在使用android messagerie app,消息存储在互联网上的CouchDB(Apache)数据库中。 如何在Android设备上使用过滤器提取消息?

Android代码段:

Replication pull = new Replication(messageDB, messageUrl, Replication.Direction.PULL);

//filter
pull.setFilter("message/by_username");
HashMap<String, Object> filterParams = new HashMap<>();
filterParams.put("username", usr);
pull.setFilterParams(filterParams);

pull.setContinuous(false); //oneshot
pull.start();

Apache设计文档:

{
  "_id": "_design/message",
  "views": {
    "by_username": {
      "map": "function (doc) {\n  emit([doc.username]);\n  \n}"
    }
  },
  "filters": {
    "by_username": "function(doc, req){ if(!doc.username) return false; if(req.query.username && req.query.username != doc.username){return false} else {return true}}"
  },
  "language": "javascript"
}

使用块过滤器,同步将永远不会停止,但数据库始终为空(3分钟后我没有找到任何文档)。

没有块过滤器,所有消息都会在几秒钟内下载。 感谢。

2 个答案:

答案 0 :(得分:0)

资料来源:https://developer.couchbase.com/documentation/mobile/current/guides/couchbase-lite/native-api/replication/index.html#filtered-pull-from-couchdb-pouchdb-or-cloudant

来自CouchDB,PouchDB或Cloudant的过滤

从Couchbase Lite 1.2开始,具有非Couchbase数据库的拉式复制中的过滤功能不再可用。在POST / {db} / _更改请求中处理filter参数的方式不兼容(请参阅#1139)。

答案 1 :(得分:0)

但是,有一种解决方法,可以在不使用如上所述的库中进行任何更改的情况下,使用带有ouchdb 2.x服务器的bedfalite lite 1.4。 请参考给出的here

答案