Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle offline firebase database when an android app is killed?

I am working on firebase for the first time, read about offline capabilities of firebase , tested two scenarios :

scenario 1 (working):

  1. offline mode, writing data to firebase database.

  2. press back button(closed app)

  3. went online, data got added to the firebase database.

scenario 2 ( not working):

  1. offline mode, writing data to firebase database
  2. close app
  3. remove app from background(killed the application)
  4. went online, data not getting added

I added this line:

Firebase.getDefaultConfig().setPersistenceEnabled(true);

how to handle scenario 2 ? Do I need to handle this scenario through local database?

like image 351
alka aswal Avatar asked Jun 20 '16 12:06

alka aswal


People also ask

Can I use Firebase as offline 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.

Can firestore work offline?

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.

How do I save and recover data from Firebase?

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.

How can I get data from Firebase without listener?

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.


Video Answer


1 Answers

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.

like image 110
Smaily Carrilho Avatar answered Oct 23 '22 10:10

Smaily Carrilho