Google Cloud Firestore Database | kotlin

Md. Imran Choudhury
Fnplus Club
Published in
4 min readMay 13, 2019

--

Google’s Cloud Firestore is NoSQL database which is a flexible, scalable database for mobile, web, and server development. Are you confused with Realtime Database?

  • Realtime Database is Firebase’s original database. It’s an efficient, low-latency solution for mobile apps that require synced states across clients in realtime.
  • Cloud Firestore is Firebase’s new flagship database for mobile app development. It improves on the successes of the Realtime Database with a new, more intuitive data model. Cloud Firestore also features richer, faster queries and scales better than the Realtime Database.

What is the difference with Real-time Database?

Firebase database was enough for basic applications. But it was not powerful enough to handle complex requirements. That is why Cloud Firestore is introduced. Here are some major changes:

  • Better querying and more structured data.
  • Write and transaction operations are atomic.
  • Reliability and performance improvements.
  • Scaling will be automatic.
  • Will be more secure.

When to use Real-time Database?

  • If your data is simple and not complex or hierarchical.
  • If you need to scale under 100,000 concurrent connections 1000 writes/second in a single database.
  • When you need a database which charges for overall bandwidth and storage and not individual operations performed.

When to use Cloud Firestore database?

  • If your data is complex or hierarchical.
  • If your data scales over 100,000 concurrent connections.
  • If you need a more precise spending limit and a database which charges primarily on 1 are PicCollage, Fabulous, Shazam and Skyscanner.

Cloud Firestore Key capabilities:

  • Flexibility
  • Expressive querying
  • Real-time updates
  • Offline support
  • Designed to scale

What about Pricing?

In Cloud Firestore, rates have lowered even though it charges primarily on operations performed in your database along with bandwidth and storage. You can set a daily spending limit too. Here are the complete details about billing.

Explore with a sample project:

Realtime Database stored data in JSON tree but Cloud Firestore stored data in documents which is very similar to JSON. In Cloud FireStore, Documents can contain subcollections and nested objects with complex objects.

Firestore example

In below image under ‘student_info’ collection can contain multiple documents like ‘student_list’. And ‘student_list’ document contains complex JSON filed.

Firebase Firestore configure:

At first, add your project to Firebase with your applicationId. In case you are using the first time you can follow the official doc for configuration(Click here). Under Database select Cloud Firestore and allow read/write the rule as true.

Allow permission

After firebase configuration set google-services.json configuration file into your project directory and add Gradle dependency on Android Studio.

implementation 'com.google.firebase:firebase-firestore:18.0.0'

JSON to Data class:

Based on your JSON project structure create your data class. You can also find some Android Studio plugin to convert JSON to the data class (Click here). For the given example data class is:

data class StudentInfo(
var studentList: ArrayList<StudentItem> = arrayListOf()
)

data class StudentItem(
var name: String = "",
var age: Int = 0,
var address: String = "",
var mobile_no: String = ""
)

Write Cloud Firestore data

  • You can write data manually from Firestore UI.
  • You can directly write your object:
  • And most interesting you can also write the whole data class collection.

Read Cloud Firestore data

  • Read the whole data class collection.
  • Read collection with condition

Available condition for data filter,

whereEqualTo('key', value)
whereLessThan('key', value)
whereLessThanOrEqualTo('key', value)
owhereGreaterThan('key', value)
whereGreaterThanOrEqualTo('key', value)
.....

As per my experience, it’s very usefully and easy to use. The most interesting is you don’t need to think about offline data store here complexity. For startup app don’t worry about the payment it’s almost free. And when your number of user increase then you can update your package. And you can also easily integrate Room database with it. So it’s time to move forward with Firestore.

Hopes you learn something, either a littleGoogle’s Cloud Firestore, Oh yes, you could get the code from GitHub.

--

--

Md. Imran Choudhury
Fnplus Club

Mobile application developer with 6 years of experience in Android & iOS apps development. (https://goo.gl/UAfmBd)