I am working on firebase for the first time, read about offline capabilities of firebase , tested two scenarios :
scenario 1 (working):
offline mode, writing data to firebase database.
press back button(closed app)
scenario 2 ( not working):
I added this line:
Firebase.getDefaultConfig().setPersistenceEnabled(true);
how to handle scenario 2 ? Do I need to handle this scenario through local database?
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.
Note: Offline persistence is supported only in Android, Apple, and web apps. To use offline persistence, you don't need to make any changes to the code that you use to access Firestore data.
Go to the Firebase Console, select your project and click on the Database tab. Scroll down and you will find a Realtime Database section. Click on Create database, select the Start in test mode option and then click Enable.
You can use Firebase's REST API to get access to the "raw" data. You can use any Firebase URL as a REST endpoint. All you need to do is append ". json" to the end of the URL and send a request from your favorite HTTPS client.
Are you using Firebase.getDefaultConfig().setPersistenceEnabled(true);
and keepSynced(true)
?
Because in Firebase documentation says that keepSynced(true)
it's who make the "magic" happens (together with setPersistenceEnabled(true)
):
By calling keepSynced(true) on a location, the data for that location will automatically be downloaded and kept in sync, even when no listeners are attached for that location. Additionally, while a location is kept synced, it will not be evicted from the persistent disk cache.
So, if you're not using it, you're not persisting your database locally and then when you "kill" the application, there will not be any database to query from when your app is opened again.
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