I have an application that pulls some simple data from Firebase Realtime Database, and to make things simple here is the flow that I want to have.
If phone is connected to the internet:
If phone is offline
The problem is that I don't see possible way to detect if data is available in firebase cache. So when phone is offline I cannot see if I should display the locally stored data or cache from firebase database.
This feature caches a copy of the Cloud Firestore data that your app is actively using, so your app can access the data when the device is offline. You can write, read, listen to, and query the cached data.
You can use hasChildren() to determine if a DataSnapshot has any children. If it does, you can enumerate them using forEach() . If it doesn't, then either this snapshot contains a primitive value (which can be retrieved with val() ) or it is empty (in which case, val() will return null ).
To see your current Realtime Database connections and data usage, check the Usage tab in the Firebase console. You can check usage over the current billing period, the last 30 days, or the last 24 hours.
If there's no cache, then the images will be requested from Firebase Storage, where the images can also be cached if you specified the cacheControl property in file metadata or if you is using Firebase Hosting with custom header Cache-Control for images.
Agree above answer, Firebase will do it for you, if you do it in the right way. In order to solve your problem, you should understand the difference between the two approaches of firebase offline cache - reading-from-memory and reading-from-disk.
Read this article -> https://pamartinezandres.com/lessons-learnt-the-hard-way-using-firebase-realtime-database-c609b52b9afb
in your case, just add, FirebaseDatabase.getInstance().setPersistenceEnabled(true);
to enable reading from disk
and add yourReferenceForFirebase.keepSynced(true);
to keep the data synched
then your app will work as you expected.
But, only this part: "If there is nothing in firebase cache -> show some default data stored locally in app", seems impossible.
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