Welcome back.
Let's run our application and go to Firebase Console.
Select Firestore Database.
And inside the markers collection we have three markers Italy Paris and San Francisco.
We have an error here in the view model argument type mismatch.
Actual type is a flow.
But collection which is a list of destination marker was expected.
Since we changed the type to flow list of destination, we need to change the view model.
So we updated the repository.
With the new implementation of flow.
We should update the view model here.
Inside this function load markers from Firestore we need to update the code.
Keep the markers which is a snapshot state list destination marker mutable state list of and.
Here let me introduce a new variable called Listener Active.
This is a boolean just to check if it is active or not, and we control it.
And here if listener active then return.
Otherwise continue with the ViewModel scoop dot launch here.
Remove the try catch because we used the listener active and inside it.
Start with marker repository dot.
Get all markers from Firestore dot collect latest and set the new markers.
Markers dot clear and add all markers.
Remove these codes and outside launch set the listener active equals to true.
Let me explain.
In this code we are listening for real time updates from a data stream.
Firestore in an Android ViewModel while managing the listener state to avoid duplicate subscriptions.
This function subscribes to a stream of marker data from Firestore updates the local markers list whenever
new data arrives, and ensures only one active listener exists at a time.
So listener active, and this is a very important variable that will fetch the state of a listener.
So this is a Boolean flag tracking.
Tracking whether a listener is already running.
Prevents duplicate subscriptions.
If the function is called multiple times and the ViewModel scope uses the Viewmodels coroutine scope.
ViewModel with which automatically cancels when the ViewModel is cleared.
Ensuring the listener doesn't leak when the ViewModel is destroyed.
Get all markers from stream.
Cancels the previous collection if a new data arrives before the previous emission is preceded, and
the process only processes that latest data.
Avoiding black backlog.
Build up markers dot clear.
Resets the existing list markers.
Dot add all replaces with fresh data.
Set the listener state to active.
Marks the listener as active to prevent redundant subscriptions.
So why collect latest.
Collect latest ideal for UI updates where only the latest data matters.
For example the map markers.
In this case, if a new data arrives while processing the current emission, it cancels the current
work and restarts with the new data.
Use collect if you need to process every emission sequentially.
The thread safety markers list should be thread safe.
Collection accessed from multiple threads.
And for this we used the markers list which is a snapshot state list.
Okay, so this is our ViewModel and this is our load markers from Firestore.
Now let's run our application in Firestore.
We have three markers Italy Paris France and San Francisco Italy Paris and San Francisco.
Now let's add a new marker here.
Change the the viewing to window.
And here let me show you.
This is better.
Okay.
Now please, guys, watch the magic.
Add a new document, which is a new coordinates for a marker.
And set the marker ID, for example.
Um, the field ID or.
No need to specify the id.
We can start directly with latitude and longitude.
So latitude which is a number.
Set the value 51.5072 and the longitude field longitude.
Please pay attention If you, uh, make any mistake here, it will not get the correct ID and the correct
longitude and latitude.
Okay, so longitude, which is of type number set the value to -0.1276.
Please pay attention here our map.
If I click save there would be a new marker in United Kingdom.
And here we go.
You see guys that this is the power of listening real time updates.
And get the latest and updated and real time data from Firebase Firestore database.
Okay guys.
So congratulations.
We succeeded in getting data and getting the real time and updated data from Firestore.