AndroidPub
Published in

AndroidPub

Firebase Cloud Firestore as new realtime database — Part 1

A realtime database sync it’s data with the connected client for any database modification. Firebase realtime database is well equipped with this capability but it’s like giant JSON tree structure when it grows big then it become difficult to maintain and understand about the data stored especially to someone new.
This issue is well addressed with the Firestore database as it’s well structured and still maintain same simplicity in usability as it was before with realtime database.

https://firebase.google.com/products/firestore/

Structure of Cloud Firestore
The entire structure is maintained by collection and document. A collection is group of document where there might be more than one document stored at any point of time whereas document is something which is used to store data and that may point to another collection.

There are few restriction on both collection and document they are
1. Collection cannot store data it can only store document.
2. Collection cannot have sub collection but it may have multiple documents.
3. Document cannot store another document but it may have sub collection which in turn may store document.
4. Each collection and document should have unique name(identifier) so that they can be accessed through unique url.
5. Supported data type that can be stored in a document are: String, Boolean, Numbers, Date, Null and Nested Arrays & Objects.

Document & Collection
A document can be considered as the storage unit where all the data is stored with the max size limit of 1 Mb.
A collection can be considered as folder which stores all the document nothing more than that. For even more simplicity if all the document of a collection gets deleted than collection also gets deleted automatically.

https://firebase.google.com/docs/firestore/data-model

One advantage with document and collection approach is that now you are free to have document with various data type so all document in a single collection need not have to follow same structure for example say a collection named animal and document1 contains information like name and country whereas in same collection document2 may contains information like name, type, county. This approach is generally not so good altogether as it may create some difficulties while retrieving data but anyways if used properly than this is also one of the good feature.

https://firebase.googleblog.com/2017/10/cloud-firestore-for-rtdb-developers.html

Write Data
There are basically two ways to send data to server they are by using add or set. It’s not necessary to specify document name everytime rather sometime it’s okay if firebase auto generate some unique name for it than in that case use add method where as when you want to give some proper name to the document than go for set method. Now this doesn’t mean that when you use set method then everytime you need to give some proper name for the document rather if no name is provided for document than it will auto create some unique document name.
Now the prime question is what’s the difference between both this methods if both have capability to create unique document name. Well the answer is here code gets reduced when you go for add method, no need to write collection(“animal”).document()…. rather collection(“animal”).add… will do the work.

//Code with add method, firebase auto create unique document name
val
hashMap = HashMap<String, Any>()
hashMap.put("name","dog")
hashMap.put("age",5)

FirebaseFirestore.getInstance().collection("animal")
.add(hashMap)
.addOnCompleteListener { task ->
if
(task.isSuccessful) {
......
} else {
.....
}
}
//Code with set method, firebase auto create unique document name
val
hashMap = HashMap<String, Any>()
hashMap.put("name","dog")
hashMap.put("age",5)
FirebaseFirestore.getInstance()
.collection("animal")
.document()
.set(hashMap)
.addOnCompleteListener { task ->
if
(task.isSuccessful) {
......
} else {
.....
}
}

Here it’s clear from the above example that how add and set methods are different from each other. So the proper usage of set method is

val hashMap = HashMap<String, Any>()
hashMap.put("name","dog")
hashMap.put("age",5)

FirebaseFirestore.getInstance()
.collection("animal")
.document("dogInfo")
.set(hashMap)
.addOnCompleteListener { task ->
if
(task.isSuccessful) {
......
} else {
.....
}
}

Just need to give some name for the document and than use set method to write data but one issue with this is that what if I need to add some fields to existing document instead of overwriting the exiting document. With above example it will always overwrite the existing document but there is a way to avoid that also to figure it out checkout the second part of this article.

--

--

--

The (retired) Pub(lication) for Android & Tech, focused on Development

Recommended from Medium

The Open Source Community Has No Place for Disrespect

Simplify Deployment, Operations and Reduce Costs with Fission

Step 3: Practice exam problems — of 5 Steps to pass your Kubernetes exam

COTI is introducing a new Community node: Meet the STARS Node

What is Technical Writing?

How to root Maximus Max991

Root LG Phone

Using Daily’s video chat API with Anvil

What is Alexa?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pankaj Rai

Pankaj Rai

Software Engineer | GDE Android & Firebase | YouTuber — All Techies

More from Medium

Twilio Android Client + Google Speech API

How does Cloud Firestore work?

Authenticating on Android with the AppAuth Library

Integration of Huawei Push Notification with android Work Manager in Navigation Glove IoT…