What actually happens when persistence is enabled in Firebase?
It's actually pretty simple. When you attach an observer (whether using observeEventType
or observeSingleEventOfType
), Firebase will:
- Immediately raise events with any complete cached data.
- 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 aValue
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!