我能够连接同步gateway到couchbase server。我有一个考虑,比如couchbase lite被用作本地database或windows应用程序的独立database。
需要澄清:
couchbase lite (本地应用数据库)是如何与同步网关连接的?这是由Couchbase lite提供的API完成的吗?发布于 2016-06-30 08:18:11
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连接到同步网关
发布于 2016-07-30 00:23:47
Couchbase Lite是一个嵌入式数据库。它支持许多平台,包括移动平台和桌面平台。
Couchbase Lite是一个完整的、功能齐全的数据库,可以单独使用.这意味着您的应用程序必须包括Couchbase Lite库代码。
在使用同步网关时,Couchbase Lite在幕后使用REST调用执行复制。实际上,您可以使用REST调用直接使用同步网关。
Couchbase Mobile developer门户上有代码示例。下面是Java中的一个片段(注意:在Android上,使用AndroidContext而不是JavaContext):
// 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();https://stackoverflow.com/questions/38114367
复制相似问题