Chapters

Hide chapters

Saving Data on Android

First Edition · Android 10 · Kotlin 1.3 · AS 3.5

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

Using Firebase

Section 3: 11 chapters
Show chapters Hide chapters

14. Realtime Database Offline Capabilities
Written by Dean Djermanović

So far, you’ve created an app that enables you to save posts to the database and read the posts from the database. The next step is to implement offline support. If you turn off your internet connection now and run your app, you’d see an empty screen. That is because your app can’t fetch the data from the database without an internet connection.

One of the most important features of Realtime database is its offline capabilities. If you were creating your own backend system, you would need to persist the data by yourself. Firebase handles that for you, and it enables your app to work properly even when the user loses network connection. In this chapter, you’ll learn how exactly it does that, and you’ll add offline support to your app so that you could see posts on the screen and even add posts while you are offline.

Setting up Firebase

If you skipped previous chapters, you need to set up Firebase in order to follow along. Do the following steps:

  1. Create a project in the Firebase console.
  2. Enable Google sign-in.
  3. Set security rules to the test mode to allow everyone read and write access.
  4. Add google-service.json to both starter and final projects.

If you need a reminder of how to do this, go back to Chapter 11: “Firebase Overview” and Chapter 12: “Introduction to Firebase Realtime Database.”

Be sure to use the starter project from this chapter, by opening the realtime-database-offline-capabilities folder and its starter project from the projects folder, rather than continuing with the final project you previously worked on. It has a few things added to it, including placeholders for the code to add in this chapter.

Enabling disk persistence

Before you start, build and run the starter project, located in the projects folder into realtime-database-offline-capabilities. Make sure you device is connected to the internet. You’ll see your posts on the home screen. Open any post. Now, disconnect your mobile device from the network and navigate back to the home screen. Your posts are still there. By default, Firebase stores your data in-memory. Now, close the app and kill the app process from the Recent apps menu. Run your app again. Now, you’ll see an empty screen.

Caching data locally

To enable disk persistence, you only need one line of code. Open WhatsUpApplication class and enable persistence on the FirebaseDatabase instance in the onCreate method:

override fun onCreate() {
    super.onCreate()
    FirebaseDatabase.getInstance().setPersistenceEnabled(true)
}

Turn on the network connection on your device. Build and run your app. Your posts will appear on the home screen. Now, disconnect your mobile phone from the network, close the app and kill the app process from the Recent apps menu. Run your app again. You’ll see the posts appearing on the home screen.

Setting the setPersistenceEnabled method argument to true enables the app to store the data to the devices local storage — the disk. That is what makes the data available even after you kill the app.

The reason you had to enable disk persistence in the Application class, instead of the RealtimeDatabaseManager, is that setPersistenceEnabled method needs to be called once per app and before creating the first database reference.

Writing data when offline

Disconnect your mobile device from the network and run your app. Add a new post. You’ll see that the post appears on the home screen like it was added to the database. But how is that possible if you are offline?

Open the Firebase console and check if your post is there. You’ll notice that it’s not. Now, connect your mobile phone to the network and observe the database data in the console. You’ll see that a few moments after you connect your app back to the internet your post appears in the database.

Setting the setPersistenceEnabled method argument to true also keeps track of all the writes you initiated while you were offline and then, when network connection comes back, it resends all the write operations. This makes the user experience optimal even if the user loses the network connection for a moment, because your app works as if it’s connected to the internet because it uses local data from the disk for synchronization.

Keeping data in sync

Realtime database stores a copy of the data, locally, only for active listeners. To understand this, first, delete your app’s data by going to your device’s Settings ▶︎ Apps & notifications ▶︎ WhatsUp ▶︎ Storage and finally tap on the Clear Storage button.

Next, build and run your app while making sure you’re connected to the internet. When the home screen opens and posts show up disconnect the device from the Internet, once again. Next, open any post that you know has comments. You’ll notice that there are no comments displayed even if you instructed the app to store data locally by setting setPersistenceEnabled(true). Since Realtime database stores data locally only for active listeners your comments weren’t saved, because you haven’t accessed them yet.

If you want to save data locally for the location that has no active listeners attached, you can use the keepSynced method on a database reference, which you’ll do in a moment.

First, open RealtimeDatabaseManager class and remove private modifier from the COMMENTS_REFERENCE constant.

Now, open WhatsUpApplication class and call keepSynced on comments reference passing in the true as an argument:

FirebaseDatabase.getInstance().apply {
      setPersistenceEnabled(true)
      getReference(COMMENTS_REFERENCE).keepSynced(true)
}

Now, the Realtime database will download the comments and keep them in sync, even if there are no active listeners at that location. Whatever happens at this location — either data gets deleted or updated, you’ll receive an update locally, as well.

Connect your mobile device to the network, and build and run your app. When posts appear on the home screen disconnect your app from the network. Now open any post that you know has comments. You’ll see your comments there, this time.

Default cache size is 10MB, which allows you to store a substantial amount of data locally, and, in most cases, this should be enough. If you exceed that limit, any data that hasn’t been used for a long time will be deleted. So it’s basically an LRU-cache-kind-of mechanism.

In a multi-user app, if there are two users that are not connected to the internet, and both write a post, one later than the other, when they finally connect to the internet, they will end up in a race-condition. Whichever user has a better and a faster connection will write to the database first. This is important to know because, in some cases, this may not be the desired behavior.

Other offline scenarios and network connectivity features

Firebase has many features that can help you when offline mode and connectivity are an important part of your app. The features you’re about to learn apply to your app regardless if the local offline persistence is enabled, or not, in your app.

Real-time presence system

The real-time presence system allows your app to know the status of your users — are they online, offline, away, or some other status. This feature is inevitable for chat applications for example, because you want to know if the person you are texting is online. This feature may seem simple, but to build an entire app infrastructure or a mechanism, which handles this for you, can be quite troublesome.

Firebase has this infrastructure implemented and it allows you to use it out of the box. Firebase saves the user presence status info to the <database>/.info/connected location that you can observe just like any other location in the database. The .info/connected reference just contains a boolean which indicates if the client is connected or not. The problem appears if you want to write something to the database when the user status changes to the offline status. For this case, you can use the special onDisconnect() method which Firebase provides, that tells the Firebase server to do something when it notices that the client isn’t online anymore. The onDisconnect() method works properly, even in cases when the app crashes, or the connection is lost, or any other nasty edge case.

On Android, Firebase automatically manages connection state to optimize battery usage and reduce bandwidth. If the client app doesn’t have any connection to the database, no active listeners, no pending requests, or similar, Firebase will automatically close the connection after 60 seconds of inactivity. Alternatively, you can explicitly close the connection by using the goOffline method.

Visit the official documentation to learn more about the presence feature.

Latency

Generally speaking, latency is the time delay between the cause and the effect of some change. In Realtime database that would, for example, be when a user triggers the action to disconnect from the server until the user is actually disconnected, or the delay between requesting a login entry, to actual authorization response.

Firebase handles latency in a way that it stores a timestamp, that is generated on the server, as data, when the client disconnects, and lets you use that data to reliably know the exact time when the user disconnected. You can combine this feature with the onDisconnect() method, that you learned about earlier, to store the exact time when the user disconnected from the database. The timestamp is a static field in the ServerValue class and you access it by calling ServerValue.TIMESTAMP.

Key points

  • Realtime database allows you to enable disk persistence to make your data available when you’re offline.
  • Enabling disk persistence also tracks of all the writes you initiated while you were offline and then when network connection comes back it synchronizes all the write operations.
  • Realtime database stores a copy of the data, locally, only for active listeners. You can use the keepSynced method on a database reference to save data locally for the location that has no active listeners attached.
  • Firebase provides you with the real-time presence system, which allows your app to know the status of your users, are they online or offline.
  • Firebase handles latency in a way that stores a timestamp, that is generated on the server, as data when the client disconnects and lets you use that data to reliably know the exact time when the user disconnected.

Where to go from here?

In this chapter, you learned how Firebase works offline and what features it provides to help you handle offline mode and connectivity issues. You also improved your apps user experience in a way that you enabled your app to work as expected even if the user is not connected to the internet.

Visit the official documentation here https://firebase.google.com/docs/database/android/offline-capabilities for more info and examples on enabling offline capabilities. You can play around with the app a bit and try to add more cool features, that Firebase provides, to the app.

Chapter 15, “Usage and Performances,” is the last chapter about Realtime database and it will teach you more about Realtime database performance and its limits.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.