Welcome back.
In this video we're going to learn how to fetch the updated and real time data from Firestore.
So we're going to use something called Flow in Android Jetpack Compose.
Using flow instead of just list is beneficial when working with asynchronous and reactive data streams.
So instead of returning a list of destination, we're going to learn here how to use the flow of list
of destination.
So what is flow?
Flow is a called asynchronous stream from Kotlin coroutines that emits values over time.
It's part of Kotlin's coroutines library and is designed to handle sequential emissions of data in a
non-blocking way.
The key characteristics of flow asynchronous can emit values over time without blocking the UI thread.
Reactive streams automatically notifies observers.
Compose UI when data changes.
Lifecycle aware can be collected safely within compose using collect as state or collect as state with
lifecycle functions.
Why we use flow.
List of destination instead of list.
Destination marker for dynamic data updates.
Coroutines integration and compose UI reactively.
Let's start with dynamic data updates.
If your list destination can change over time.
So as I show you in the previous video, if the list is updated over time, for example, you add new
markers to the database flow allows the UI to actively update when the list changes.
But if you use list no, you can't get the real time updates from the database.
And this is what happened in the previous videos.
Okay, without flow, you would need manual callbacks or live data to notify changes.
The second characteristic is coroutines.
Integration flow works seamlessly with coroutines, making it ideal for background operations.
For example, fetching data from a database or API compose UI.
Reactively, Jetpack Compose can collect airflow and automatically recompose when a new data is emitted.
Using collect as state function.
Whenever new marker are added to your database, the flow will emit and notify UI to recompose and render
and rerenders the UI with the newly updated list.
Flow is cancelable, meaning it stops emitting when the composable leaves the composition, avoiding
memory leaks.
Okay, so if you have a static data, static and never changes, you can use simply a list of destination
markers.
So for example, if you have like changes, specific markers and there is no change for them and you
don't need to re to fetch them in a real time way, you can use list of destination marker.
But if you need to fetch the data in a real time updated manner, you use flow.
If the data is loaded once from the ViewModel, you might use the list of destination marker.
But if you are listening for real time updates, you should use flow.