首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于couchbase lite和同步网关

关于couchbase lite和同步网关
EN

Stack Overflow用户
提问于 2016-06-30 05:06:22
回答 2查看 300关注 0票数 0

我能够连接同步gatewaycouchbase server。我有一个考虑,比如couchbase lite被用作本地database或windows应用程序的独立database

需要澄清:

  1. couchbase lite (本地应用数据库)是如何与同步网关连接的?这是由Couchbase lite提供的API完成的吗?
  2. 有谁有简单的java代码来帮助获取诸如如何与db连接以及如何使用java代码处理数据库之类的东西?
EN

回答 2

Stack Overflow用户

发布于 2016-06-30 08:18:11

代码语言:javascript
复制
private URL createSyncURL(boolean isEncrypted){
    URL syncURL = null;
    String host = "https://10.0.2.2";  //sync gateway ip
    String port = "4984";              //sync gateway port
    String dbName = "couchbaseevents";
    try {
        syncURL = new URL(host + ":" + port + "/" + dbName);
    } catch (MalformedURLException me) {
        me.printStackTrace();
    }
    return syncURL;
}

private void startReplications() throws CouchbaseLiteException {
    Replication pull = this.getDatabaseInstance().createPullReplication(this.createSyncURL(false));
    Replication push = this.getDatabaseInstance().createPushReplication(this.createSyncURL(false));
    pull.setContinuous(true);
    push.setContinuous(true);
    pull.start();
    push.start();
}

当您调用startReplications方法时,它将内部调用createSyncURL,后者将couchbase db连接到同步网关

票数 0
EN

Stack Overflow用户

发布于 2016-07-30 00:23:47

Couchbase Lite是一个嵌入式数据库。它支持许多平台,包括移动平台和桌面平台。

Couchbase Lite是一个完整的、功能齐全的数据库,可以单独使用.这意味着您的应用程序必须包括Couchbase Lite库代码。

在使用同步网关时,Couchbase Lite在幕后使用REST调用执行复制。实际上,您可以使用REST调用直接使用同步网关。

Couchbase Mobile developer门户上有代码示例。下面是Java中的一个片段(注意:在Android上,使用AndroidContext而不是JavaContext):

代码语言:javascript
复制
// Get the database (and create it if it doesn’t exist).
Manager manager = new Manager(new JavaContext(), Manager.DEFAULT_OPTIONS);
Database database = manager.getDatabase("mydb");

// Create a new document (i.e. a record) in the database.
Document document = database.createDocument();
Map properties = new HashMap();
properties.put("firstName", "John");
document.putProperties(properties);

// Update a document.
document.update(new Document.DocumentUpdater() {
    @Override
    public boolean update(UnsavedRevision newRevision) {
        Map properties = newRevision.getUserProperties();
        properties.put("firstName", "Johnny");
        newRevision.setUserProperties(properties);
        return true;
    }
});

// Delete a document.
document.delete();

// Create replicators to push & pull changes to & from the cloud.
URL url = new URL("https://www.my.com/mydb/");
Replication push = database.createPushReplication(url);
Replication pull = database.createPullReplication(url);
push.setContinuous(true);
pull.setContinuous(true);

// Add authentication.
Authenticator authenticator = AuthenticatorFactory.createBasicAuthenticator(name, password);
push.setAuthenticator(authenticator);
pull.setAuthenticator(authenticator);

// Listen to database change events (there are also change
// events for documents, replications, and queries).
database.addChangeListener(this);

// Start replicators
push.start();
pull.start();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38114367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档