Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

javascript - Firebase synchronisation of locally-modified data: handling errors & global status

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:

Google Drive screenshot

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?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...