我正在尝试将我的 CouchDB 与我的 android 同步,使其离线工作与在线一样好,据我所知 .continues(true/false) 是实现此目的的方法。但是有没有办法在运行时以某种方式实际改变它?我正在考虑让 BroadcastReciever 监听是否有任何 CONNECTIVITY_SERVICE 并相应地设置 .continues() 的值,但我无法让它工作......有什么建议吗?
protected void startEktorp() {
httpClient = new TouchDBHttpClient(server);
dbInstance = new StdCouchDbInstance(httpClient);
HamsterSyncEktorpAsyncTask startupTask = new HamsterSyncEktorpAsyncTask() {
@Override
protected void doInBackground() {
// create a local database
dbConnector = dbInstance.createConnector(DATABASE_NAME, true);
}
@Override
protected void onSuccess() {
// Create the query and get the results
queryDB();
// Start doing the replications!
startReplications();
}
};
startupTask.execute();
}
public synchronized void queryDB() {
ViewQuery viewQuery = new ViewQuery()
.designDocId("_design/" + dDocName).viewName(viewName)
.reduce(false).includeDocs(true);
ViewResult viewResult = dbConnector.queryView(viewQuery);
Log.v("SUCCESS", viewResult.getRows().toString());
notifyHandlers(viewResult);
}
/**
* Replicates the database from server to the phone and vice versa.
*/
public void startReplications() {
// pull this database to the test replication server
pullCommand = new ReplicationCommand.Builder().source(DATABASE_PATH)
.target(DATABASE_NAME).continuous(true).build();
pushCommand = new ReplicationCommand.Builder().source(DATABASE_NAME)
.target(DATABASE_PATH).continuous(true).build();
HamsterSyncEktorpAsyncTask pushReplicaton = new HamsterSyncEktorpAsyncTask() {
@Override
protected void doInBackground() {
dbInstance.replicate(pullCommand);
ReplicationStatus status = dbInstance.replicate(pushCommand);
Log.e(TAG, status.getSessionId().toString());
}
};
pushReplicaton.execute();
}
我还想知道是否有任何方法可以知道复制是否以及何时实际发送/获取新内容并在应用程序中更新我的列表视图,以显示我的数据库中的所有文档(queryDB() 函数)?