TachyonVortex February 2016

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 se

Answers


Frank van Puffelen February 2016

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.

Post Status

Asked in February 2016
Viewed 3,505 times
Voted 7
Answered 1 times

Search




Leave an answer