The Journey of Firebase 🔥 — Realtime Database
Hello guys, in the past post we show an overview about what Firebase is, if you don’t saw yet, please check it here.
So, now it’s time to talk about the services of firebase and how to implement them in an app, in this case we will talk about the Firebase Realtime Database.
What is Realtime Database?
The Firebase Realtime Database is a cloud-hosted NoSQL database that lets you store and sync data between your users in realtime.
To set up the firebase and use the Realtime Database on your android project, we need to create an Firebase project at Firebase Website.
After that you need to add the json file and the dependencies on your project, like that.
So for this journey I created this basic project to understand in pratique, you can see all the code and download it on my Github.
Save data to Realtime Database
For save data to Realtime Database, first you need to create a model.
On your activity(in my case) or fragment, initialize the FirebaseDatabase and the DatabaseReference.
On your button, set the action to save the data on Realtime Database.
So that is it, you can see the complete file here.
Retrieve data from Realtime Database
First you need to create your ViewHolder file and extends from RecyclerView.ViewHolder.
Create your the RecyclerAdapter that will extends from FirebaseRecyclerAdapter and implement the onCreateViewHolder and onBindViewHolder method.
On the activity(in my case)or fragment for retrieve data, create an method to instantiate the FirebaseRecyclerOptions and the viewHolder that we have created. After that set the adapter to the recyclerview (rv).
Don’t forget to initialize the FirebaseDatabase and DatabaseReference.
override the onStart() and onStop() method to call the startListening() and stopListening() listener.
Delete data from Realtime Database
For delete a row, in the button for delete, you just need to call the removeValue() method from DatabaseReference and passing the key in my case the phone.
Update data from Realtime Database
For update a value, in the button for update, you just need to call on your button action the setValue() method from DatabaseReference and passing the data you want to update.
Just it too.
Thanks to you for reading this post! Please do 👏 if you liked it and want more posts about firebase 🔥 and android development.
Hello guys, today we will start our journey to understand the Firebase platform and the services their bring to all…medium.com