gpt4 book ai didi

javascript - 本地修改数据的 Firebase 同步 : handling errors & global status

转载 作者:数据小太阳 更新时间:2023-10-29 06:10:37 24 4
gpt4 key购买 nike

我有两个关于 Firebase web platform 的相关问题的 synchronisation of locally-modified data to the server :

Every client sharing a Firebase database maintains its own internal version of any active data. When data is updated or saved, it is written to this local version of the database. The Firebase client then synchronizes that data with the Firebase servers and with other clients on a 'best-effort' basis.


1。处理同步错误

数据修改方法( set() , remove() , ETC)可以接受一个 onComplete 回调参数:

A callback function that will be called when synchronization to the Firebase servers has completed. The callback will be passed an Error object on failure; else null.

var onComplete = function(error) {
if (error) {
console.log('Synchronization failed');
} else {
console.log('Synchronization succeeded');
}
};

fredRef.remove(onComplete);

在上面的示例中,fredRef.remove() 回调应该接收什么样的错误?

  • 临时错误?
    • 客户端离线(网络连接丢失)?
    • Firebase 服务器暂时过载或停机维护,但很快就会恢复可用?
  • 永久性错误?
    • 权限被拒绝(由于 security rules)?
    • 数据库位置不存在?

有没有办法区分暂时性错误和永久性错误?

我们应该如何处理这些错误/从这些错误中恢复?

对于临时错误,我们是否需要在短时间后再次调用 fredRef.remove() 以重试操作?


2。全局同步状态

我意识到每次调用 set()remove() 都会收到一个单独的同步成功/失败消息导致 onComplete 回调。但我正在寻找一种方法来确定整个 Firebase 客户端的全局同步状态

我想使用 beforeunload事件监听器在所有修改的数据同步到服务器之前,当用户试图离开页面时警告用户,我正在寻找类似 firebase.isAllModifiedDataSynced() 的函数。像这样:

window.addEventListener('beforeunload', function (event) {
if (!firebase.isAllModifiedDataSynced()) {
event.returnValue = 'Some changes have not yet been saved. If you ' +
'leave this page, your changes will be lost.';
}
});

以下是 Google 云端硬盘中相同功能的示例:

Google Drive screenshot

我知道 special /.info/connected location :

it is useful for a client to know when it is online or offline. Firebase clients provide a special location at /.info/connected which is updated every time the client's connection state changes. Here is an example:

var connectedRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/.info/connected");
connectedRef.on("value", function(snap) {
if (snap.val() === true) {
alert("connected");
} else {
alert("not connected");
}
});

特殊的 /.info/connected 位置可以像这样连接到 beforeunload 事件监听器:

var connectedRef = new Firebase('https://myapp.firebaseio.com/.info/connected');
var isConnected = true;

connectedRef.on('value', function (snap) {
isConnected = snap.val();
});

window.addEventListener('beforeunload', function (event) {
if (!isConnected) {
event.returnValue = 'Some changes have not yet been saved. If you ' +
'leave this page, your changes will be lost.';
}
});

我的问题是:

  • 如果isConnectedtrue这是否也意味着所有修改的数据都已同步到服务器?
  • “已连接”是否也意味着“已同步”

如果不是,应用程序如何确定整个 Firebase 客户端的全局同步状态?

  • 是否有特殊的 /.info/synchronized 位置?
  • 应用程序是否需要手动跟踪每个 onComplete 回调的同步成功/失败结果?

最佳答案

In the example above, what kind of errors should the fredRef.remove() callback expect to receive?

Client is offline (network connection lost) ?

不,这不会导致将错误传递给完成监听器。它只会导致(尚未)调用完成监听器。

Firebase server is temporarily overloaded or down for maintenance, but will be available again soon?

没有。这与没有网络连接本质上是一样的。

Permission denied (due to security rules) ?

是的,这确实会导致将错误传递给完成处理程序。

Database location does not exist?

不,这不会导致完成监听器发生错误。

If isConnected is true, does this also mean that all modified data has been synced to the server? i.e. Does "connected" also mean "synced"?

不,它没有。 .info/connected 将在与数据库建立连接时以 true 触发。

If not, how can the app determine the global sync status of the whole Firebase client?

目前无法确定您的本地数据是否与服务器保持同步。

Is there a special /.info/synchronized location?

不,这样的位置不存在。

Does the app need to manually keep track of the sync success/failure result of every onComplete callback?

这取决于用例。但是,如果您只想知道何时执行所有写入,请推送一个虚拟值并等待其完成。由于 Firebase 按顺序执行写入,您可以确定在那个阶段您已经获得了其他事件。

关于javascript - 本地修改数据的 Firebase 同步 : handling errors & global status,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35280502/

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