Welcome back.
We used dry sand and a way to close functions, but there are errors here.
None of the following candidates is applicable because of receiver type mismatch.
Okay.
And if we hover here unresolved reference trace.
This type of error occurs because these functions need to be called within the proper coroutine context
or because you are missing necessary imports.
Let's try to use the callback.
So here equals to callback flow and scroll down.
The errors are solved.
But what are try send and await close functions and why we use them in Kotlin coroutines, particularly
when working with flows and callback based APIs like Firebase Firestore.
Try sand and await close serve specific purposes to bridge between callback style APIs and reactive
streams flow in this this function.
Get all markers from Firestore.
We used the reactive streams of the flow and the callback style API, which is the listener, and the
Firebase collection.
The bridge between them is try and await close functions.
Try send non-blocking emission of values to a flow from a callback.
I want from you to write those notes down.
The purpose of try sand is non-blocking emission of values to a flow from a callback used in callback
flow or mutable shared flow to safely emit values without suspending the coroutine.
Why we use it Firebase's ad snapshot listener uses callbacks, but we want to expose data as a flow.
Try send allows emitting values from the callback into the flow.
Non suspending regular sand suspends if the buffer is full, dry sand is non-blocking and returns immediately
true if the value was emitted successfully.
False if the buffer is full or the flow is closed, a wait close suspends the coroutine until the flow
is canceled, then runs clean up code, ensuring resources like Firebase Firestore listeners are released
when the flow is no longer needed.
Resource cleanup means the callback based APIs like Firestore listeners need to be unregistered to avoid
leaks.
Await close provides a way to run cleanup when the flow collection stops.
Flow lifetimes in Jetpack Compose flows collected via collect as a state function are canceled when
the composable leaves the composition and await close function ensures that Firebase Firestore listener
is removed at the right time.
So here the coroutine suspends the await close until either the flow is canceled.
Triggered the cleanup block or close is called explicitly.
And here we need to use close and use the error.
Okay.
So this will cancel the flow and close it.
Okay.
One last thing we need to solve is the return type of the flow.
Here we change it from list of destination marker to flow of list of destination marker.
Let me go back to the marker repository and change it to a flow of list of destination marker.
Import flow.
And here we go.
So this is the usage of wait close and try send functions.