Firebase Database Offline Capabilities | Android Studio | Kotlin

Firebase Offline Capabilities for Firebase Database

What if there is a temporary internet issue with your device? What if you don't want to load your database data again & again? Firebase easily allows you to achieve this task by persisting the data locally, managing presence and handling the latency. The data is available offline even if you restart the app or device. It will behave the same as you're online. Firebase keeps a queue of all write operations that are performed while your application is offline e.g. unstable internet connection. When your app becomes online all the queued write operations are sent to the Firebase Realtime Database Server.

Firebase Database Offline Capabilities | Android Studio | Kotlin

>>Check For Java

Enabling Firebase Offline Persistence

Once you enable the Disk Persistence then the firebase handles the job automatically. You just need to add the following code snippet in your Application Class.

MyApplication.java

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        ////enable firebase offline capabilities
        Firebase.database.setPersistenceEnabled(true)

    }
}

Keep Required Data Refresh

What if you want to keep some data synced? This is easily possible as you can keep specific locations synced even if the firebase database persistence is enabled. 

val usersRef = Firebase.database.getReference("Users")
usersRef.keepSynced(true)

You can turn the synchronization off later with the following code snippet.

usersRef.keepSynced(false)


Comments

Popular posts from this blog

Manage External Storage Permission | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Java

Add a Back Button to Action Bar Android Studio (Kotlin)