Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What actually happens when persistence is enabled in Firebase?

When turning on persistence for Firebase in iOS what actually happens to my observers and how they behave on a technical level?

I have a good idea how it should work on a high-level based on this https://firebase.google.com/docs/database/ios/offline-capabilities - Firebase essentially keeps a cached copy of the data you can access whilst offline.

What I don't understand is how many times my observers should fire and with what information.

  • Does firebase always trigger my observers once with any cached data first (or null if there isn't any data) followed by the server data.
  • Or does it only send the cached data if it exists followed by the server data.
  • Is there any difference between observerSingleValue and a continous observer's behaviour when in persistence mode ?

In our app with persistence enabled, I have noticed:

  • Firebase just sending the server data
  • Firebase sending the cached data if it exists then the server data.
  • Firebase sending the cached data and null if it doesn't exist followed by the server data.

It would be good to clear this up so we know which should be the normal behaviour :)

like image 548
James Campbell Avatar asked May 17 '16 12:05

James Campbell


People also ask

Is Firebase persistent?

Note that Firebase Auth web sessions are single host origin and will be persisted for a single domain only. Indicates that the state will only persist in the current session or tab, and will be cleared when the tab or window in which the user authenticated is closed.

How can Firebase be used with Android platform for data persistence?

Persistence BehaviorThe Firebase Realtime Database client automatically keeps a queue of all write operations that are performed while your app is offline. When persistence is enabled, this queue is also persisted to disk so all of your writes are available when the user or operating system restarts the app.

How do you delete data from Firebase Realtime Database?

The simplest way for deleting data is to call removeValue() on a reference to the location of that data. We can also delete data by specifying null as the value for another write operation such as setValue() or updateChildren().

How do I save Firebase data offline?

Configure offline persistence When you initialize Cloud Firestore, you can enable or disable offline persistence: For Android and Apple platforms, offline persistence is enabled by default. To disable persistence, set the PersistenceEnabled option to false . For the web, offline persistence is disabled by default.


1 Answers

It's actually pretty simple. When you attach an observer (whether using observeEventType or observeSingleEventOfType), Firebase will:

  1. Immediately raise events with any complete cached data.
  2. Request updated data from the server and, when it arrives, raise new events if the data is different than what was cached.

There are a couple subtleties that fall out of this though:

  • We'll only raise events with cached data if it is complete. This means:
    • If we have no cached data (you haven't observed this location before), we will not raise events with null or similar. You won't get any events until we get data from the server.
    • If you have partial data for this location (e.g. you observed /foo/bar previously but now you're observing /foo), you will get ChildAdded events for complete children (e.g. /foo/bar), but you won't get a Value event (e.g. for /foo) until we've gotten complete data from the server for the location you're observing.
  • If you're using observeSingleEventOfType, you're explicitly asking for only a single event and so if you have cached data, #1 will happen but #2 will not, which may not be what you want (you'll never see the latest server data).

Hope this helps!

like image 169
Michael Lehenbauer Avatar answered Oct 06 '22 00:10

Michael Lehenbauer