Offline Android App with Cloud Firestore

Sandeep Sankla
3 min readAug 19, 2021

--

This quickstart shows you how to store data remotely, easily access them and monitor the changes in Real-time.

Cloud Firestore

Cloud Firestore is a cloud-hosted, NoSQL database that your application (Android, iOS, and web apps) can access directly via native SDKs. Like the Firebase Real-time Database, it keeps syncing our data via real-time listeners to the client app. It provides offline support for mobile and web so we can create responsive apps that work regardless of network latency or Internet connectivity.

Oh, I got it. You are thinking How it differs from Firebase's real-time Database. So here is the answer to your question. I hope, now you get more clarity of Firebase Vs Firestore after reading the answer.

Here is the link to how to set up an Android project with cloud Firestore

In this post, you will learn how to send data from the app to the cloud, how to access that data & real-time data changes with Kotlin +MVVM pattern.

Hope you all set up the app Gradle for cloud firestore, Now let's start our interesting journey. To make an offline app you follow three steps

  1. Decide the data Schema
  2. Update/Push the data on firestore cloud
  3. Add listener in the app to monitor the real-time data changes

Let's discuss each point in detail:

  1. Schema — In my sample app I have taken a sample schema of a Person, Very basic details of the person like -id, name, and city you can add as many you need, here is the sample data

2. Pushing Data on Cloud - To add document on Cloud you just need the collection reference of firestore and set the data, here is the sample code for that

3. Listener — You can listen to a document with the onSnapshot() method. The listener will help you observe the real-time changes in the document and helps to perform the necessary action. Here in our application, I am using addSnapshotListener with the callback — DocumentChange.Type.ADDED, DocumentChange.Type.REMOVED, DocumentChange.Type.MODIFIED, So on each document(Person), added we are simply emitting the data & populating it in the list. for more details about the listener — read this

Hope you enjoy this article, you can find the project here, Attaching a video for reference that will show you about document changes. You can make a chat -messaging app using firestore cloud as well. It’s my first article please mention it in the comment if you need more details of this.

Summary: In this article, you have learnt to use Firestore cloud real-time DB with android architecture(MVVM) pattern.

Clap is highly recommended for motivation.

--

--

Sandeep Sankla

Tech Lead (Mobile) at Park+. Passionate about mobile development and learning.