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.
In our app with persistence enabled, I have noticed:
It would be good to clear this up so we know which should be the normal behaviour :)
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.
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.
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().
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.
It's actually pretty simple. When you attach an observer (whether using observeEventType
or observeSingleEventOfType
), Firebase will:
There are a couple subtleties that fall out of this though:
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.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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With