WEBVTT

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

00:00.960 --> 00:07.520
In this video we're going to learn how to fetch the updated and real time data from Firestore.

00:07.720 --> 00:13.840
So we're going to use something called Flow in Android Jetpack Compose.

00:14.040 --> 00:21.960
Using flow instead of just list is beneficial when working with asynchronous and reactive data streams.

00:22.240 --> 00:30.640
So instead of returning a list of destination, we're going to learn here how to use the flow of list

00:30.680 --> 00:32.000
of destination.

00:32.080 --> 00:33.880
So what is flow?

00:33.920 --> 00:43.440
Flow is a called asynchronous stream from Kotlin coroutines that emits values over time.

00:43.640 --> 00:52.880
It's part of Kotlin's coroutines library and is designed to handle sequential emissions of data in a

00:52.920 --> 00:54.240
non-blocking way.

00:54.360 --> 01:02.890
The key characteristics of flow asynchronous can emit values over time without blocking the UI thread.

01:03.050 --> 01:08.050
Reactive streams automatically notifies observers.

01:08.210 --> 01:10.690
Compose UI when data changes.

01:10.890 --> 01:18.690
Lifecycle aware can be collected safely within compose using collect as state or collect as state with

01:18.690 --> 01:20.530
lifecycle functions.

01:20.770 --> 01:22.090
Why we use flow.

01:22.130 --> 01:25.250
List of destination instead of list.

01:25.250 --> 01:28.810
Destination marker for dynamic data updates.

01:28.850 --> 01:33.490
Coroutines integration and compose UI reactively.

01:33.730 --> 01:36.530
Let's start with dynamic data updates.

01:36.690 --> 01:40.610
If your list destination can change over time.

01:40.610 --> 01:50.050
So as I show you in the previous video, if the list is updated over time, for example, you add new

01:50.050 --> 01:57.810
markers to the database flow allows the UI to actively update when the list changes.

01:58.010 --> 02:05.180
But if you use list no, you can't get the real time updates from the database.

02:05.180 --> 02:08.740
And this is what happened in the previous videos.

02:08.780 --> 02:16.700
Okay, without flow, you would need manual callbacks or live data to notify changes.

02:16.860 --> 02:19.420
The second characteristic is coroutines.

02:19.420 --> 02:27.460
Integration flow works seamlessly with coroutines, making it ideal for background operations.

02:27.460 --> 02:32.060
For example, fetching data from a database or API compose UI.

02:32.100 --> 02:40.340
Reactively, Jetpack Compose can collect airflow and automatically recompose when a new data is emitted.

02:40.340 --> 02:42.940
Using collect as state function.

02:42.940 --> 02:55.420
Whenever new marker are added to your database, the flow will emit and notify UI to recompose and render

02:55.580 --> 02:59.620
and rerenders the UI with the newly updated list.

02:59.620 --> 03:08.540
Flow is cancelable, meaning it stops emitting when the composable leaves the composition, avoiding

03:08.540 --> 03:09.980
memory leaks.

03:10.180 --> 03:20.180
Okay, so if you have a static data, static and never changes, you can use simply a list of destination

03:20.180 --> 03:20.820
markers.

03:20.820 --> 03:28.940
So for example, if you have like changes, specific markers and there is no change for them and you

03:28.940 --> 03:36.660
don't need to re to fetch them in a real time way, you can use list of destination marker.

03:36.660 --> 03:44.980
But if you need to fetch the data in a real time updated manner, you use flow.

03:45.020 --> 03:52.860
If the data is loaded once from the ViewModel, you might use the list of destination marker.

03:52.860 --> 03:57.620
But if you are listening for real time updates, you should use flow.
