WEBVTT

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

00:01.040 --> 00:07.480
We talked about flow and how important it is for fetching real time updates.

00:07.520 --> 00:13.240
We said that flow is a called asynchronous stream that emits values over time.

00:13.280 --> 00:20.320
We need to put real time data in this flow in order to return this function.

00:20.360 --> 00:24.000
A flow of list of destination markers.

00:24.040 --> 00:34.720
So from where we need to get the real time updates, this code gets the data once when the app launches.

00:34.720 --> 00:44.600
And this is a problem if there is a new marker inserted into database during the app run.

00:44.640 --> 00:48.680
This new marker will not be represented on the map.

00:48.680 --> 00:52.880
And this is what we've seen in the previous videos.

00:53.000 --> 01:01.540
Here we need to listen for real time updates from Firestore collection, which is the marker for this.

01:01.820 --> 01:03.340
Remove this code.

01:03.420 --> 01:05.380
This code the previous code.

01:06.140 --> 01:10.900
Get the data once during the runtime.

01:10.900 --> 01:14.820
We don't need to get only one time.

01:14.820 --> 01:19.540
We need to fetch and get real time data changes.

01:19.540 --> 01:26.700
For this use val listener equals to firebase dot collection.

01:26.940 --> 01:29.820
Set the collection the same as before.

01:30.020 --> 01:36.940
And here the newly used code is add snapshot listener.

01:37.100 --> 01:44.300
Here we are listening for real time updates from Firestore collection named markers.

01:44.460 --> 01:54.820
Whenever the data in markers changes added, modified or removed, the snapshot callback is triggered.

01:55.100 --> 01:59.540
If an error occurs, new network issues permission denied.

01:59.780 --> 02:02.990
The error callback is executed.

02:03.030 --> 02:09.390
Okay, so this is the idea behind getting the real time updates.

02:09.670 --> 02:13.470
Okay, so whenever data changes, snapshot is triggered.

02:13.470 --> 02:19.030
Whenever an error occurs, the error callback is executed.

02:19.070 --> 02:27.230
As we said before, the collection of Firebase Firestore is named markers that add the snapshot listener

02:27.550 --> 02:32.270
attaches a real time listener to the collection.

02:32.270 --> 02:38.550
So here we are attaching a real time listener for the collection.

02:38.550 --> 02:44.270
I want from you to focus with me and write the notes down.

02:44.270 --> 02:52.110
Because these notes are very important and help you in memorizing every code.

02:52.270 --> 02:58.030
Okay, here we are attaching a real time listener to the collection.

02:58.070 --> 03:02.890
The snapshot a query snapshot containing the latest data.

03:02.930 --> 03:07.090
Error an exception if something went wrong.

03:07.290 --> 03:10.370
Let me continue with this code here.

03:10.570 --> 03:17.170
Let me start with if start with if error is not null.

03:17.410 --> 03:23.490
So if there is an error what we need to do return add snapshot listener.

03:23.490 --> 03:31.090
And this is error dot print stack trace and return and snapshot listener.

03:31.130 --> 03:37.490
It exits the listener callback early to avoid processing invalid data.

03:37.530 --> 03:40.250
This is in case of an error.

03:40.410 --> 03:44.290
Now let's handle if there is no error.

03:44.330 --> 03:46.170
Now use.

03:46.210 --> 03:54.130
If snapshot does not equal to null and snapshot is not empty.

03:54.130 --> 03:58.930
So there is no null values and it's not empty.

03:59.290 --> 04:06.620
Let me use vowel markers equals to snapshot dot map.

04:06.740 --> 04:12.180
We used map in the previous videos, but I'll show you here.

04:12.220 --> 04:15.660
Fetched marker or my marker.

04:15.660 --> 04:17.420
You can use my marker.

04:17.420 --> 04:22.660
And here let me customize it as a destination marker okay.

04:22.700 --> 04:24.540
Here like this.

04:24.540 --> 04:28.620
And let me explain what we've done here.

04:28.620 --> 04:32.660
Here we are checking if snapshot is valid.

04:32.780 --> 04:41.300
It's not null ensures that query snapshot Firebase response is not null and snapshot is not empty.

04:41.580 --> 04:44.860
Checks if the snapshot contains documents.

04:45.060 --> 04:54.620
Avoids processing empty results if both conditions pass, the snapshot has valid data to process and

04:54.620 --> 04:59.820
convert Firestore documents to destination marker objects.

04:59.880 --> 05:10.040
Snapshot dot map iterates iterates over over each document query document snapshot in the snapshot and

05:10.040 --> 05:13.760
maps it to a destination marker.

05:13.760 --> 05:23.160
So every document, every marker in the markers list would be customized into a destination marker object.

05:23.320 --> 05:30.560
The fields marker ID marker that got double latitude longitude markers dot get double longitude and

05:30.560 --> 05:32.840
marker dot get string that title.

05:33.040 --> 05:39.000
So here we are getting every document in the snapshot.

05:39.160 --> 05:48.080
Go and create a destination marker and fetch the IDs, the latitude, longitude and title variables

05:48.080 --> 05:50.120
from this document.

05:50.160 --> 05:59.480
Now we need to emit map data via try sent here outside the if block.

05:59.730 --> 06:09.530
Use dry sand markers, sending the mapped list destination marker to the flow consumers, for example,

06:09.530 --> 06:17.370
Jetpack Compose UI and part of the callback flow or mutable shared flow API, which is used to bridge

06:17.370 --> 06:20.850
callback based APIs like Firebase Firestore.

06:20.890 --> 06:32.090
Listeners with Kotlin Flow and we can handle empty or invalid snapshots using try or else try send an

06:32.090 --> 06:45.690
empty list, then remove this return and here use await close and set the listener remove in the preview.

06:45.770 --> 06:52.370
In the next videos, we're going to learn about the callback and how to solve those errors and how to

06:52.410 --> 06:54.010
import the correct package.
