Couchdb CouchbaseLite等待拉复制完成
我有一个CouchDB,我正在通过CouchBaseLite 1.4连接。在继续应用程序之前,我很难等待提取所有文档 目前,我正在以一种非常粗糙的方式实现这一点,我希望将其修复为更好地符合适当的编码标准 当前:Couchdb CouchbaseLite等待拉复制完成,couchdb,couchbase-lite,Couchdb,Couchbase Lite,我有一个CouchDB,我正在通过CouchBaseLite 1.4连接。在继续应用程序之前,我很难等待提取所有文档 目前,我正在以一种非常粗糙的方式实现这一点,我希望将其修复为更好地符合适当的编码标准 当前: pull.setContinuous(false); pull.start(); //Waits for pull replication to start pulling in data while(!pull.isRunning()); //Waits for pull repl
pull.setContinuous(false);
pull.start();
//Waits for pull replication to start pulling in data
while(!pull.isRunning());
//Waits for pull replication to finish.
while(!pull.getStatus().equals(Replication.ReplicationStatus.REPLICATION_STOPPED));
//Set continuous to true
pull.setContinuous(true);
//Start it again.
pull.start();
我这样做的原因是我可能需要等待数据库中的2个文档,如果它们不存在,桌面应用程序将进入安装模式
- 是否有办法等待所有文档完成拉取
没有黑双
而
呢
- 更好的是,假设我知道文档的
。有没有办法等到两者都被拉出来后再继续\u id
// Replication.ChangeListener
@Override
public void changed(Replication.ChangeEvent changeEvent) {
if (changeEvent.getError() != null) {
Throwable lastError = changeEvent.getError();
// React to the error
return;
}
if (changeEvent.getTransition() == null) return;
ReplicationState dest = changeEvent.getTransition().getDestination();
replicationActive = ((dest == ReplicationState.STOPPING || dest == ReplicationState.STOPPED) ? false : true);
// Do something here if true
}
您可以对数据库对象上的更改侦听器执行类似的操作,以便在复制两个特定文档时进行捕获
由于听起来您希望在其他地方进行初始设置后,这些文档会出现在数据库中,因此另一种方法是执行一次性复制以获取第一批文档,然后在完成后开始连续复制。这并不能完全解决我的问题,作为一个
changeListener
不会阻止我的主线程运行。我同意你的第二个建议,并使用了一次复制来获取这两个文档。它现在运行速度明显加快。虽然我仍然使用double while,但我只在等待两个文档,所以暂停是最小的,可以接受的,即使代码很难看。还有一些其他想法:通过回调中的调用获取主执行,或者将线程置于睡眠状态并从回调发出信号?