Firebase 同步本地修改数据:处理错误和全球地位 [英] Firebase synchronisation of locally-modified data: handling errors & global status

查看:23
本文介绍了Firebase 同步本地修改数据:处理错误和全球地位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个关于 :

<块引用>

客户端知道它何时在线或离线很有用.Firebase 客户端在 /.info/connected 提供了一个特殊位置,每次客户端的连接状态更改时都会更新该位置.下面是一个例子:

var connectedRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/.info/connected");connectedRef.on("value", function(snap) {if (snap.val() === true) {警报(已连接");} 别的 {警报(未连接");}});

特殊的 /.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) {如果 (!isConnected) {event.returnValue = '某些更改尚未保存.如果你 ' +'离开此页面,您的更改将丢失.';}});

我的问题是:

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

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

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

解决方案

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

客户端离线(网络连接丢失)?

不,这不会导致将错误传递给完成侦听器.它只会导致尚未调用完成侦听器.

<块引用>

Firebase 服务器暂时过载或停机以进行维护,但很快就会再次可用?

没有.这与没有网络连接的情况基本相同.

<块引用>

权限被拒绝(由于安全规则)?

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

<块引用>

数据库位置不存在?

不,这不会导致完成侦听器出错.

<块引用>

如果 isConnected 为真,这是否也意味着所有修改的数据都已同步到服务器?即是否连接"?也意味着同步"?

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

<块引用>

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

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

<块引用>

是否有特殊的/.info/synchronized 位置?

不,这样的位置不存在.

<块引用>

应用是否需要手动跟踪每个 onComplete 回调的同步成功/失败结果?

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

I have two related questions regarding the Firebase web platform's 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. Handling sync errors

The data-modification methods (set(), remove(), etc) can take an onComplete callback parameter:

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

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

  • Temporary errors?
    • Client is offline (network connection lost) ?
    • Firebase server is temporarily overloaded or down for maintenance, but will be available again soon?
  • Permanent errors?
    • Permission denied (due to security rules) ?
    • Database location does not exist?

Is there a way to distinguish between temporary and permanent errors?

How should we handle / recover from these errors?

For temporary errors, do we need to call fredRef.remove() again after a short period of time, to retry the operation?


2. Global sync status

I realise that each call to set() and remove() will receive an individual sync success/failure result in the onComplete callback.  But I'm looking for a way to determine the global sync status of the whole Firebase client.

I'd like to use a beforeunload event listener to warn the user when they attempt to leave the page before all modified data has been synced to the server, and I'm looking for some function like firebase.isAllModifiedDataSynced().  Something like this:

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.';
    }
});

Here's an example of the same functionality in Google Drive:

I'm aware of the 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");
  }
});

The special /.info/connected location can be connected to a beforeunload event listener like this:

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.';
    }
});

My question is:

  • 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"?

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?

解决方案

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

Client is offline (network connection lost) ?

No, this will not cause an error to be passed to the completion listener. It will simply cause the completion listener to not be called (yet).

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

No. This is essentially the same as being without a network connection.

Permission denied (due to security rules) ?

Yes, this is will indeed cause an error to be passed to the completion handler.

Database location does not exist?

No, this will not cause an error to be caused to the completion listener.

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"?

No it does not. .info/connected will fire with true when a connection is made to the database.

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

There is currently no way to determine whether your local data is up to date with the server.

Is there a special /.info/synchronized location?

No, such a location doesn't exist.

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

That depends on the use-case. But if you want to simply know when all your writes are executed, push a dummy value and wait for that to complete. Since Firebase executes the writes in order, you can be certain at that stage that you've gotten the other events.

这篇关于Firebase 同步本地修改数据:处理错误和全球地位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆