What Actually Happens When Persistence Is Enabled in Firebase

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:

  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!

firebase transactions after restart with persistence enabled

Transactions are not persisted to disk. So when you app is restarted, none of your transactions will be sent to the server.

After regaining connectivity, your local cache will contain the data from the server.

2 Firebase instances in Android: One with persistence and one without

You can use SYNC feature of Firebase. Just turn it off by adding keepSynced(false) and will do the job.

Brief Description:

Firebase synchronizes and stores a local copy of the data for active listeners. In addition, you can keep specific locations in sync.

Copy

Firebase scoresRef = new Firebase("https://dinosaur-facts.firebaseio.com/scores");
scoresRef.keepSynced(true);

The client will automatically download the data at these locations and keep it in sync even if the reference has no active listeners. You can turn synchronization back off with the following line of code.

Copy

scoresRef.keepSynced(false);

By default, 10MB of previously synced data will be cached. This should be enough for most applications. If the cache outgrows its configured size, Firebase will purge data that has been used least recently. Data that is kept in sync, will not be purged from the cache.

Link

Just how persistent are Firebase database observers?

While the listener may stay active in your single test case, you should not rely on Firebase Database listeners for background data delivery.

The listener stays active as long as the connection stays open. It is up to the operating system to determine when it closes the connection. There is a good chance that the iOS emulator has different behavior in this case than a physical device would have.

Can I have Firebase database disk persistence enabled for selective children and for other not?

When using the following line of code in your project:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

It means that all data from the Realtime Database that the user recently received is cached to the disk.

I do not know how to implement it for one child only for example and the others be available only online.

There is no way you can exclude certain nodes from that disk persistence. So you cannot choose whether a node should be or shouldn't be cached on the disk. It's all or nothing.



Related Topics



Leave a reply



Submit