javascript - Firebase synchronisation of locally-modified data: handling errors & global status -
i have 2 related questions regarding firebase web platform's synchronisation of locally-modified data server:
every client sharing firebase database maintains own internal version of active data. when data updated or saved, written local version of database. firebase client synchronizes data firebase servers , other clients on 'best-effort' basis.
1. handling sync errors
the data-modification methods (set()
, remove()
, etc) can take oncomplete
callback parameter:
a callback function called when synchronization firebase servers has completed. callback passed
error
object on failure; elsenull
.var oncomplete = function(error) { if (error) { console.log('synchronization failed'); } else { console.log('synchronization succeeded'); } }; fredref.remove(oncomplete);
in example above, kind of errors should fredref.remove()
callback expect receive?
- temporary errors?
- client offline (network connection lost) ?
- firebase server temporarily overloaded or down maintenance, available again soon?
- permanent errors?
- permission denied (due security rules) ?
- database location not exist?
is there way distinguish between temporary , permanent errors?
how should handle / recover these errors?
for temporary errors, need call fredref.remove()
again after short period of time, retry operation?
2. global sync status
i realise each call set()
, remove()
receive individual sync success/failure result in oncomplete
callback. i'm looking way determine global sync status of whole firebase client.
i'd use beforeunload
event listener warn user when attempt leave page before modified data has been synced server, , i'm looking function firebase.isallmodifieddatasynced()
. this:
window.addeventlistener('beforeunload', function (event) { if (!firebase.isallmodifieddatasynced()) { event.returnvalue = 'some changes have not yet been saved. if ' + 'leave page, changes lost.'; } });
here's example of same functionality in google drive:
i'm aware of special /.info/connected
location:
it useful client know when online or offline. firebase clients provide special location @
/.info/connected
updated every time client's connection state changes. here 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 connected beforeunload
event listener 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 ' + 'leave page, changes lost.'; } });
my question is:
- if
isconnected
true
, does mean modified data has been synced server? - i.e. "connected" mean "synced"?
if not, how can app determine global sync status of whole firebase client?
- is there special
/.info/synchronized
location? - does app need manually keep track of sync success/failure result of every
oncomplete
callback?
in example above, kind of errors should fredref.remove() callback expect receive?
client offline (network connection lost) ?
no, not cause error passed completion listener. cause completion listener not called (yet).
firebase server temporarily overloaded or down maintenance, available again soon?
no. same being without network connection.
permission denied (due security rules) ?
yes, indeed cause error passed completion handler.
database location not exist?
no, not cause error caused completion listener.
if isconnected true, mean modified data has been synced server? i.e. "connected" mean "synced"?
no not. .info/connected
fire true when connection made database.
if not, how can app determine global sync status of whole firebase client?
there no way determine whether local data date server.
is there special /.info/synchronized location?
no, such location doesn't exist.
does app need manually keep track of sync success/failure result of every oncomplete callback?
that depends on use-case. if want know when writes executed, push dummy value , wait complete. since firebase executes writes in order, can @ stage you've gotten other events.
Comments
Post a Comment