Work with Firebase Realtime database lists
Lets learn with example how we can listen list items updates in firebase realtime database
Intro 🎤
Today in this blog we are going to learn how we can read data in Firebase Realtime Database(RTDB) with use case. This might be sound like old topic, but still I want to share how we are using RTDB in our app and use realtime data changes from RTDB.
Before getting started!
If you are new to RTDB, you can check below Firebase guide, how you can configure your database and add into your app:
Now let’s take a look at reading data from Firebase Realtime Database
Use cases:
We are developing event platform and in app we have below use cases to get the realtime database, to display on event participants side which there are live in any of event using our app.
- Case 1: We need to show realtime live users count from RTDB where we are getting current live users those are in the event.
- Case 2: During the event any of the guest participants can send request to the Event host, that participants want to speak and that request will be visible on host side as Raise hand indicator in events participants listing on host side.
So in case one we know that as single node value we can update direct node that will contain realtime value of app live participants, for this we can use ValueEventListener
where we can listen on that node that will gives us a latest value.
And in second case there is case that multiple users can send raise hand request to become a speaker, so we can use ChildEventListener
to listen child update. So as guest user if I send request child item will be added/updated and once I cancel request child item will be removed under single node e.g( raiseHandRequest
) .
ah! lot of line I have read, time for some line of code :)
Case 1: lets see how we have implemented getting realtime live user count from database:
- Add database ValueEventListener on realtime database node to get value of user counts:
Firebase.database.reference.child(["parent_node"]).child("[Event_node]")
.child("liveUsers")
.addValueEventListener(liveUserCountListener)
Add ValueEventListener
:
private val liveUserCountListener = object :
ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val count = snapshot.getValue<Long>()
updateLiveCount(count)
}
override fun onCancelled(error: DatabaseError) {
}
}
private fun updateLiveCount(count: Long?) {
Logger.log("Live Users ->", count.toString()) // here we are updating live user count view
}
Remove ValueEventListener
:
Firebase.database.reference.child(["parent_node"]).child("[Event_node]")
.child("liveUsers")
.removeEventListener(liveUserCountListener)
Case 2: Let’s see how we have implemented realtime Raise hand request items value that we are updating in list on host side. Initially we are getting all data once user open users screen. Once new request will be send by any guest users, we are listening in child event listener, where we are going to update our list as per child added/removed or update in list node:
Firebase.database.reference.child(["parent_node"]).child("[Event_node]")
.child("raiseHandRequests")
.addChildEventListener(object :
ChildEventListener {
override fun onCancelled(databaseError: DatabaseError) {
}
override fun onChildAdded(
dataSnapshot: DataSnapshot,
previousChildName: String?
) {
// if new child added in raiseHandRequests node
val newChildAdded = dataSnapshot.getValue<EventParticipantModel>()
updateRealtimeValueForRaiseHand(newChildAdded)
}
override fun onChildChanged(
dataSnapshot: DataSnapshot,
previousChildName: String?
) {
// if new child changed in raiseHandRequests node
val newChildAdded = dataSnapshot.getValue<EventParticipantModel>()
updateRealtimeValueForRaiseHand(newChildAdded)
}
override fun onChildRemoved(snapshot: DataSnapshot) {
// if child removed from raiseHandRequests node
val newChildRemoved = snapshot.getValue<EventParticipantModel>()
updateRealtimeValueForRaiseHand(newChildRemoved,isForDelete = true)
}
override fun onChildMoved(
snapshot: DataSnapshot,
previousChildName: String?
) {
}
})
Then in list update the value as per received trigger, if it is for add/update(in the case if user send raise hand request) or it is for delete item(in the case if user revert raise hand request)
private fun updateRealtimeValueForRaiseHand(newChildAdded:EventParticipantModel?,isForDelete:Boolean=false){//delete item from list if it is "isForDelete" case else add item in the list
}
Wrap Up!
That was pretty cool, right? This is all the stuff you need to know about how we can implement Firebase realtime database to read data in your Android app.
Summary:
Let’s just take a look at the summary of what we have learned:
- How we can read data from firebase database on any changes
- How we can read direct node changes in the app
- How we can read list of child changes in the app
Resources:
- https://firebase.google.com/docs/database/android/read-and-write
- https://firebase.google.com/docs/database/android/lists-of-data
🙏 Thanks for reading this article. Be sure to clap/recommend as much as you can and also share with your friends. It means a lot to me. Checkout more blogs at https://pranaypatel.medium.com/