Bil*_*ble 4 database-replication pouchdb
我的PouchDB同步代码没有产生完整的事件.
我确实得到了更改以及活动和暂停事件(按此顺序),并且数据库最终会同步(在很长时间的等待之后,即使数据不多).
我需要完整的事件,所以我知道何时本地可以使用远程CouchDB数据.
我的代码看起来像这样:
init(localDBName, remoteDBName)
// Initialises the database - Called every time the App starts or the user logs in or registers
// If remoteDBName is false we don't want to sync data to the server
{
// Create a new database or open it if it already exists
this._DB = new PouchDB(localDBName);
this.remote = remoteDBName; // If remoteDBName is false we don't sync data to the server
console.log('Data: init(): PouchDB database opened for localDBName = ' + localDBName + ' : ' + this._DB + ', remoteDBName = ' + this.remote);
// Sync to remote DB if we have been asked to
if (this.remote)
{
// Insert the url for the CouchDB server into the remoteDBName returned by PouchDB
var realRemoteDB = this.remote.replace("localhost:5984", COUCHDB_SERVER_URL);
console.log('Data: init: remoteDB path being used is: ' + realRemoteDB);
let options = {
live: true,
retry: true,
continuous: true
};
return this._syncHandler = this._DB.sync(realRemoteDB, options)
.on('complete', (info) => {
console.log('***** DATA: init() Complete: Handling syncing complete');
console.dir(info);
})
.on('change', (info) => {
console.log('***** DATA: init() Change: Handling syncing change');
console.dir(info);
})
.on('paused', (info) => {
console.log('***** DATA: init() Paused: Handling syncing pause');
console.dir(info);
})
.on('active', (info) => {
console.log('***** DATA: init() Active: Handling syncing resumption');
console.dir(info);
})
.on('error', (err) => {
console.log('***** DATA: init() Error: Handling syncing error');
console.dir(err);
})
.on('denied', (err) => {
console.log('***** DATA: init() Denied: Handling syncing denied');
console.dir(err);
});
}
else {
this.syncHandler = null;
}
}Run Code Online (Sandbox Code Playgroud)
我得到的最后一个事件是暂停事件,该事件在更改事件显示已从服务器提取数据后触发.
Pho*_*log 10
这实际上是它应该工作的方式;)
sync根据您指定的选项,该方法的行为会有所不同:
没有live选项:在正常的同步(没有live选项)中,complete事件将按照您的想法运行:一旦同步完成,它将触发.
使用live选项:在实时复制/同步中,同步将永远不会完成,因为它是连续的.complete只有在取消复制时才会触发该事件.请参阅此处的文档(我强调了重要部分):
complete(info) - 复制完成或取消时将触发此事件.在实时复制中,仅取消复制应触发此事件.
要解决您的问题,您最初可以在没有live选项的情况下进行同步.如果此同步完成,则远程数据应在本地可用.
现在,在complete事件处理程序中,您可以启动实时复制以启动连续同步.
代码可能如下所示:
this._DB.sync(realRemoteDB) //No options here -> One time sync
.on('complete', (info) => {
this._DB.sync(realRemoteDB, options); //Continous sync with options
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1934 次 |
| 最近记录: |