What do I need to do in here in terms of closing/c...
# coroutines
f
What do I need to do in here in terms of closing/cancellation/throwing? This is so confusing. Do I need to close the Flow? Cancel the coroutine? Anything else?
Copy code
inline fun <reified T> Query.asFlow() = callbackFlow<List<T>> {
    val registration = addSnapshotListener { snapshot, error ->
        if (error != null) {
            cancel(CancellationException("Query snapshot error", error))
        }
        offer(snapshot?.toObjects(T::class.java) ?: emptyList())
    }
    awaitClose { registration.remove() }
}
a
use cancel for consumer telling producer to stop, use close for producer telling consumer that there will be no more (with or without an exception)
f
ok, thank you
although the docs use
cancel
inside the callback in their callbackFlow example
l
These docs might need to be improved… maybe submit an issue there?
f
well I can't if don't know what's right
😅 3
l
I don't think it's correct to use
cancel()
inside a
callbackFlow
.
1