The Journey of Firebase 🔥 — Cloud Firestore
We are back, for one more post about the Firebase Services, so in this post we will talk about the Cloud Firestore and compare them with Realtime Database.
The Cloud Firestore
Cloud Firestore is a NoSQL document database that lets you easily store, sync, and query data for your mobile and web apps — at global scale.
With Firestore, instead of storing data as rows and columns, it is stored as a collection of documents. It also features richer, faster queries and scales better than the Realtime Database.
Documents
In Cloud Firestore, the unit of storage is the document. A document is a lightweight record that contains fields, which map to values. Each document is identified by a name.
Collection
Documents live in collections, which are simply containers for documents.
Cloud Firestore is schemaless, so you have complete freedom over what fields you put in each document and what data types you store in those fields.
A collection contains documents and nothing else. It can’t directly contain raw fields with values, and it can’t contain other collections.
Realtime Database vs Cloud Firestore
Both Realtime Database and Cloud Firestore are NoSQL Databases.
You can see more difference between Realtime Database and Cloud Firestore Here.
Setup Project
To use Cloud Firestore in our project First we need to add its dependency.
So for this post I added some activities to the project that I previously created in the previous post. You can see all the code and download it on my Github
Save data to Cloud Firestore
To save data first create the model.
After that in your fragment or activity (in my case) create an instance of Firestore, set the collection (Person), after that we create the instance of our model with the data that comes from our view and add it to the collection.
On the collection you can see that we have two methods, on the addOnSuccessListener you can put all logic if the write succeeds and on the addOnFailureListener you can put all logic if the write fails.
That’s just it.
Retrieve data from Cloud Firestore
To retrieve data from cloud Firestore, first we need to create the Adapter.
After that in your fragment or activity (in my case) create a list of Person, instantiate the adapter (PersonFirestoredapter), instantiate the RecyclerView and the FirebaseFirestore.
So, to get the data, get the collection (Person) and call the get() method. On addOnSuccessListener check if the query is not empty to populate the lis of Person and don’t forget to call the notifyDataSetChanged() method in adapter.
Delete data from Cloud Firestore
First in Holder pass the List of Person and the context as parameter, implement also the onClickListener Interface.
After that, override the onClick method to start a new activity when the user clicks on item, and pass the person on determinate position as extra.
In your fragment or activity (in my case) instantiate the db(FirebaseFirestore) and get the Person (Model) that comes as an extra.
then get the colletion(Person), in the colletion get the document for determinated Id and then call the delete method, on the addOnSuccessListener you can put all logic if the write succeeds and on the addOnFailureListener you can put all logic if the write fails.
That's Just It
Update data from Cloud Firestore
To update data from Cloud Firestore, First do the First & the second step from the delete.
After that fill out fields with the data that comes from from Firestore get as extra.
Instantiate the person (Model) with the values that come from the view, get the collection (Person), get the document passing the id and call the set method passing the person (Model).
Also, on the addOnSuccessListener you can put all logic if the write succeeds and on the addOnFailureListener you can put all logic if the write fails.
Note, that you can call the update method instead set, to update one or all values.
So, that is it for now and don’t forget to see the full code here.
Thanks to you for reading this post! Please do 👏 if you liked it and want more posts about firebase 🔥 and android development.