Brijesh Chauhan
Brijesh Chauhan

Reputation: 19

Is there a way to save Firebase real-time database data when an Internet connection is not available?

I am making an attendance Android app in Kotlin. In this app, a students list is available offline with an SQLite database.

For attendance saving, I am using Firebase Realtime Database.

But, I want to make app which can work in offline mode also. And upload attendance when an Internet connection is available, even after restarting the application.

Upvotes: -1

Views: 668

Answers (2)

Naveen Kumar Sangwan
Naveen Kumar Sangwan

Reputation: 39

To enable offline persistence in Firebase Realtime Database, you can use the following code:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

This line of code should be placed before any other Firebase Realtime Database operations in your app, preferably in the onCreate() method of your MainActivity or in your Application class.

Upvotes: -1

Alex Mamo
Alex Mamo

Reputation: 1

IMHO, I cannot see any benefit of implementing this mechanism since the Realtime Database has its own offline persistence mechanism. According to the official documentation:

Firebase apps automatically handle temporary network interruptions. Cached data is available while offline and Firebase resends any writes when network connectivity is restored.

When you enable disk persistence, your app writes the data locally to the device so your app can maintain its state while offline, even if the user or operating system restarts the app.

So while offline, you can continue using the app. In Android, this can be simply done using:

Firebase.database.setPersistenceEnabled(true)

Upvotes: 1

Related Questions