Question

From Firebase's FAQ:

What happens to my app if I lose my network connection?

Firebase transparently reconnects to the Firebase servers as soon as you regain connectivity. In the meantime, all Firebase operations done locally by your app will immediately fire events (...). Once connectivity is reestablished, you’ll receive the appropriate set of events so that your client “catches up” with the current server state

Then what happens if I go offline and keep modifying my local data, then come back online and other clients have performed different changes? Which one will ultimately be saved?

If the data on the server gets overridden, does it mean older data can replace newer one?

If the newer data added online is kept, do I know that the data submitted while offline has been discarded?

Was it helpful?

Solution

When your client comes back online, after an offline period and writing data, the behavior of those changes will be determined by which method you used to write them:

  • The set(), setWithPriority(), remove(), and push() methods are last-write-wins. This means that if offline client A makes a change at t=0, and online client B makes a change at t=10, that offline client A's changes will overwrite client B's changes when upon reconnection. Note that this specifically applies to the changes that were made (i.e. set /a/b/c to 1), not the entire Firebase.
  • The transaction() method, however, is built specifically for handling conflicts. When offline client A reconnects, your transaction update function will re-run and apply the new change to your Firebase data.

In most applications, users are appending data to lists or modifying individual user state, but not modifying the same piece of data. In the event that multiple users are modifying the same piece of data, you'll want to use transaction() whether you're offline or not.

Generally speaking, Firebase has been built to handle going offline and online automatically and so you shouldn't have to write application code to detect and handle that case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top