‫We created the chat message model class.
‫Now let's move to the repository and create a function that returns for us immutable live data of list
‫of chat message model class.
‫For that I'll create mutable live data, list of chat message and I'll name it as messages live data.
‫And let's initialize this message.
‫Live data equals to new mutable live data.
‫In the constructor.
‫Let's scroll down and I'll create a getter method for this.
‫Messages.
‫Live data I want from you to focus with me.
‫Now I'll get the messages, the chat messages according to the group name.
‫So for that I need to pass here in the parameters a string called group name.
‫So I'll fetch the chat messages according to the passed parameter, which is the group name, and I'll
‫use this group name in order to detect and specify the database reference.
‫So for that, I'll not use the database reference that we created before.
‫I'll create another reference.
‫So database reference I'll name it as group reference.
‫Scroll down and back to the Get Messages live data method.
‫And here group reference equals to database I'll use the database Firebase database dot get reference
‫dot child.
‫And here I need to pass the group name.
‫Database is the Firebase database instance which is obtained from Firebase SDK or library get reference.
‫This method is used to get a reference to a location in the Firebase Realtime database.
‫It's typically called on database object to create a reference to the root of that database child method.
‫This is used to specify the child node or location under the root reference, and the group name is
‫a variable or string representing the name of the child, node or location.
‫In the previous examples, when we get the chat group mutable live data, we use the reference and the
‫reference.
‫If we go back, we can see it represents the root reference in the chat messages.
‫We don't need the root, we need the group.
‫So for that I use the child method.
‫And in the same way we've done before with the with the group chat groups, I'll use the list of messages
‫chat messages, model class and I'll name it as messages list, new array list.
‫And inside this list I'll fetch the data from database realtime database of the firebase and add it
‫to the mutable live data that we created before, which is the messages live data.
‫Okay, I'll use database reference or group reference here.
‫Dot add value event listener, new value event listener and on data change.
‫I need to clear the messages list at first in order to prevent any duplication of data, and I'll use
‫a for each loop for each data snapshot snapshot data snapshot.
‫And this is the updated data that we received from Firebase I'll use get children method.
‫So I'm using a for each loop to iterate through a collection of data snapshot objects obtained from
‫snapshot dot get children data snapshot.
‫This is a class typically used in Firebase to present the snapshot of data at a particular location
‫in the database, and data snapshot.
‫This is a variable that takes on the value of each item in the collection.
‫As you iterate through snapshot dot get children method.
‫This part specifies the collection of items over which the loop iterates snapshots.
‫It seems to be previously defined, variable or object representing a location or snapshot of data in
‫the Firebase Realtime database and get the children method is a method to retrieve the child snapshots
‫of that location and inside this for loop.
‫This time we need to get a chat message, model, class and objects.
‫So here I name it as message equals to data snapshot dot get value.
‫Don't need the key, we need the value.
‫And here chat message dot class data snapshot dot get value.
‫This part of code retrieves the data from the data snapshot and attempts to deserialize it into an instance
‫of.
‫Chat message class.
‫The get value method is a common method provided by Firebase or similar database libraries, to retrieve
‫the data as an object of the specified class.
‫Data snapshot this data snapshot object that contains the specific piece of data within the database
‫and get value.
‫This part tells the Firebase SDK to attempt to convert the data within the data snapshot into an instance
‫of chat message class.
‫So this is the Deserialization process in Firebase.
‫Of course I need to add to the messages list this message.
‫So I use add method we created before that mutable live data here messages live data and it's of type
‫list of chat message.
‫So for that I need to update the mutable live data with the latest list of messages.
‫So for that messages live data dot post value to messages list.
‫Of course I need to return this messages live data here.
‫Add the return type of this function is of type mutable live data.
‫List of chat message.
‫This is how we get messages.
‫Live data.