What are the trade-offs when I use sync on several paths in my Firebase database?
databaseRef.keepSynced(true);
I never clear the sync from these paths.
In addition, I might call databaseRef.keepSynced(true); several times on the same path. Is t a problem?
I'm also using
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
So what do I "pay" for this sync in terms of battery life, memory issues?
By enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache.
Firebase Hosting uses a powerful global CDN to make your site as fast as possible. Any requested static content is automatically cached on the CDN. If you redeploy your site's content, Firebase Hosting automatically clears all your cached static content across the CDN until the next request.
While the device is offline, if you have enabled offline persistence, your listeners will receive listen events when the locally cached data changes. You can listen to documents, collections, and queries.
When you use the keepSynced()
method, you're telling Firebase to download and cache all the data from databaseRef
. I hope databaseRef
isn't the root Reference of your Database because if it is, you're downloading your entire database and this is not a good practice.
You should use keepSynced()
to cache nodes that are really necessary for your app to work offline.
You will probably be wondering how is it different from setPersistanceEnabled(true)
. Well, setPersistanceEnabled(true)
only caches data when there is a Listener attached to that node (when the data has been read at least once).
On the other hand, keepSynced(true)
caches everything from that node, even if there is no listener attached.
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