https://kotlinlang.org logo
#coroutines
Title
# coroutines
f

Florian

11/24/2020, 5:27 PM
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

Adam Powell

11/24/2020, 5:29 PM
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

Florian

11/24/2020, 6:23 PM
ok, thank you
although the docs use
cancel
inside the callback in their callbackFlow example
l

louiscad

11/24/2020, 9:12 PM
These docs might need to be improved… maybe submit an issue there?
f

Florian

11/24/2020, 9:15 PM
well I can't if don't know what's right
😅 3
l

louiscad

11/25/2020, 10:17 AM
I don't think it's correct to use
cancel()
inside a
callbackFlow
.
1