gpt4 book ai didi

database-replication - PouchDB 同步没有给出完整的事件

转载 作者:行者123 更新时间:2023-12-04 08:20:19 24 4
gpt4 key购买 nike

我的 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;
}
}


我得到的最后一个事件是在更改事件显示数据已从服务器中提取后立即触发的暂停事件。

最佳答案

这实际上是它应该工作的方式;)
sync方法的行为取决于您指定的选项:

  • 没有 live选项 : 在正常同步中(没有 live 选项)complete事件将像您想象的那样工作:一旦同步完成,它将触发。
  • live选项 :在实时复制/同步中,同步永远不会完成,因为它是连续的。 complete仅当复制被取消时才会触发事件。请参阅文档 here (我强调了重要的部分):

    complete (info) - This event fires when replication is completed or cancelled. In a live replication, only cancelling the replication should trigger this event.



  • 为了解决您的问题,您最初可以在不使用 live 的情况下进行同步。选项。如果此同步完成,您的远程数据应该在本地可用。
    现在在 complete事件处理程序,您可以启动实时复制以启动连续同步。
    代码可能如下所示:
    this._DB.sync(realRemoteDB) //No options here -> One time sync
    .on('complete', (info) => {
    this._DB.sync(realRemoteDB, options); //Continous sync with options
    });

    关于database-replication - PouchDB 同步没有给出完整的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43138371/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com