2017-08-25 87 views
0

我有一个CouchDB,我通过CouchBaseLite 1.4连接。在继续执行应用程序之前,我无法等待所有文档被拖动。CouchbaseLite等待pull复制完成

目前我正以非常冒险的方式实现这一目标,并且我想根据适当的编码标准来修正它。

电流:

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(); 

的原因,我这样做是我可能必须在DB 2个文件,我需要等待,如果它们不存在的桌面应用程序,进入设置模式。

  • 有没有办法等待所有文件完成拉 没有哈克双while
  • 更好的是,让我们假设我知道_id的文档。在继续之前是否有办法等到BOTH被拉出?

回答

1

使用更改侦听器。要监视复制,你要像

// 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 
} 

你可以做与数据库对象的更改侦听器,当两个特定的文件已复制到赶上类似的东西。

因为它听起来就像你希望这些文档是在初始设置其他地方后,数据库,另一种方法是做一锤子复制得到的头文件,然后开始连续复制它后完了。

+0

这并不完全解决我的问题,因为'changeListener'不会阻止我的主线程运行。我采用了第二个建议,并使用一次性复制来抓取这两个文档。它现在运行速度明显加快。尽管我仍然使用了double,但我只是在等待两个文档,因此即使代码很丑,暂停也很少且可以接受。 –

+0

啊,是的,我通常会以无阻塞的方式思考。还有其他一些想法:通过回调中的调用来获取主执行,或者让线程进入休眠状态并从回调中发出信号? – Hod