WEBVTT

00:00.080 --> 00:00.960
Welcome back.

00:01.200 --> 00:05.400
Let's run our application and go to Firebase Console.

00:05.400 --> 00:08.240
Select Firestore Database.

00:08.240 --> 00:15.800
And inside the markers collection we have three markers Italy Paris and San Francisco.

00:15.840 --> 00:21.160
We have an error here in the view model argument type mismatch.

00:21.200 --> 00:22.920
Actual type is a flow.

00:22.920 --> 00:28.480
But collection which is a list of destination marker was expected.

00:28.600 --> 00:35.720
Since we changed the type to flow list of destination, we need to change the view model.

00:35.720 --> 00:37.840
So we updated the repository.

00:37.880 --> 00:40.160
With the new implementation of flow.

00:40.320 --> 00:44.160
We should update the view model here.

00:44.320 --> 00:51.720
Inside this function load markers from Firestore we need to update the code.

00:51.760 --> 00:59.320
Keep the markers which is a snapshot state list destination marker mutable state list of and.

00:59.320 --> 01:04.480
Here let me introduce a new variable called Listener Active.

01:04.680 --> 01:11.520
This is a boolean just to check if it is active or not, and we control it.

01:11.520 --> 01:16.400
And here if listener active then return.

01:16.440 --> 01:21.160
Otherwise continue with the ViewModel scoop dot launch here.

01:21.360 --> 01:27.720
Remove the try catch because we used the listener active and inside it.

01:27.920 --> 01:32.240
Start with marker repository dot.

01:32.440 --> 01:41.560
Get all markers from Firestore dot collect latest and set the new markers.

01:41.680 --> 01:44.800
Markers dot clear and add all markers.

01:44.800 --> 01:52.360
Remove these codes and outside launch set the listener active equals to true.

01:52.400 --> 01:53.680
Let me explain.

01:53.680 --> 01:59.560
In this code we are listening for real time updates from a data stream.

01:59.680 --> 02:07.440
Firestore in an Android ViewModel while managing the listener state to avoid duplicate subscriptions.

02:07.440 --> 02:17.090
This function subscribes to a stream of marker data from Firestore updates the local markers list whenever

02:17.090 --> 02:23.330
new data arrives, and ensures only one active listener exists at a time.

02:23.570 --> 02:33.090
So listener active, and this is a very important variable that will fetch the state of a listener.

02:33.250 --> 02:36.730
So this is a Boolean flag tracking.

02:36.810 --> 02:40.330
Tracking whether a listener is already running.

02:40.490 --> 02:43.090
Prevents duplicate subscriptions.

02:43.090 --> 02:49.810
If the function is called multiple times and the ViewModel scope uses the Viewmodels coroutine scope.

02:49.850 --> 02:56.490
ViewModel with which automatically cancels when the ViewModel is cleared.

02:56.610 --> 03:01.170
Ensuring the listener doesn't leak when the ViewModel is destroyed.

03:01.370 --> 03:03.970
Get all markers from stream.

03:04.130 --> 03:11.690
Cancels the previous collection if a new data arrives before the previous emission is preceded, and

03:11.690 --> 03:16.330
the process only processes that latest data.

03:16.370 --> 03:18.810
Avoiding black backlog.

03:18.850 --> 03:21.450
Build up markers dot clear.

03:21.490 --> 03:24.850
Resets the existing list markers.

03:24.850 --> 03:28.770
Dot add all replaces with fresh data.

03:28.770 --> 03:31.650
Set the listener state to active.

03:31.770 --> 03:37.730
Marks the listener as active to prevent redundant subscriptions.

03:37.730 --> 03:40.450
So why collect latest.

03:40.490 --> 03:47.210
Collect latest ideal for UI updates where only the latest data matters.

03:47.210 --> 03:48.810
For example the map markers.

03:48.810 --> 03:57.010
In this case, if a new data arrives while processing the current emission, it cancels the current

03:57.010 --> 04:00.490
work and restarts with the new data.

04:00.530 --> 04:05.890
Use collect if you need to process every emission sequentially.

04:06.050 --> 04:09.610
The thread safety markers list should be thread safe.

04:09.610 --> 04:12.610
Collection accessed from multiple threads.

04:12.850 --> 04:19.250
And for this we used the markers list which is a snapshot state list.

04:19.290 --> 04:25.690
Okay, so this is our ViewModel and this is our load markers from Firestore.

04:25.890 --> 04:29.530
Now let's run our application in Firestore.

04:29.570 --> 04:37.370
We have three markers Italy Paris France and San Francisco Italy Paris and San Francisco.

04:37.410 --> 04:41.170
Now let's add a new marker here.

04:41.330 --> 04:46.290
Change the the viewing to window.

04:46.490 --> 04:49.690
And here let me show you.

04:49.930 --> 04:50.890
This is better.

04:50.930 --> 04:51.410
Okay.

04:51.810 --> 04:56.050
Now please, guys, watch the magic.

04:56.210 --> 05:00.930
Add a new document, which is a new coordinates for a marker.

05:01.250 --> 05:04.970
And set the marker ID, for example.

05:05.130 --> 05:08.890
Um, the field ID or.

05:09.410 --> 05:11.650
No need to specify the id.

05:11.730 --> 05:16.610
We can start directly with latitude and longitude.

05:16.610 --> 05:19.450
So latitude which is a number.

05:19.730 --> 05:30.090
Set the value 51.5072 and the longitude field longitude.

05:30.090 --> 05:39.540
Please pay attention If you, uh, make any mistake here, it will not get the correct ID and the correct

05:39.540 --> 05:41.180
longitude and latitude.

05:41.220 --> 05:52.460
Okay, so longitude, which is of type number set the value to -0.1276.

05:52.460 --> 05:56.500
Please pay attention here our map.

05:56.500 --> 06:02.180
If I click save there would be a new marker in United Kingdom.

06:02.180 --> 06:03.260
And here we go.

06:03.300 --> 06:10.540
You see guys that this is the power of listening real time updates.

06:10.540 --> 06:18.580
And get the latest and updated and real time data from Firebase Firestore database.

06:18.620 --> 06:19.540
Okay guys.

06:19.540 --> 06:21.260
So congratulations.

06:21.260 --> 06:29.820
We succeeded in getting data and getting the real time and updated data from Firestore.
