Any reason why `emit` says it has to be ran from a...
# android
n
Any reason why
emit
says it has to be ran from a coroutine body ( I am well aware it is a suspend function)?
Copy code
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

class FirebaseDataSource (private val firebaseFirestore: FirebaseFirestore) : NetworkDataSource{

    fun fetchMenuItems(): Flow<Food> = flow{
        firebaseFirestore.collection("menu_items")
                .get()
                .addOnSuccessListener { documents ->
                    try {
                        for (document in documents) {
                            val doc =...
                            emit(doc) //error here
                        }
                    } 
                }
    }
In every example I've seen online they have it just inside the
flow {}
Eg.
Copy code
fun simple(): Flow<Int> = flow { 
    for (i in 1..3) {
        delay(100)          
        println("Emitting $i")
        emit(i)
    }
}
t
That's probably an issue with context preservation: you are probably emitting after the end of the
flow
block has been reached. Two suggestions here: 1. write an equivalent of
get()
that suspends. You can do that using
suspendCancellableCoroutine
. 2. Use
callbackFlow
.
a
should be able to call
await
after the
get()
to convert the Task to suspending e.g.
val documents = firebaseFirestore.collection("menu_items").get().await()
if not, then you can write your own
suspendCancellableCoroutine
as mentioned above
m
The lambda expression that you are passing to
flow
is a coroutine body. However, the
OnSuccessListener
lambda expression is not part of that coroutine body. That converts into a separate function on a separate object and lives separate from the
Flow
.
callbackFlow()
would allow you to adapt this callback-based API to a `Flow`: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/callback-flow.html Or, there seem to be some existing Firebase-with-coroutines libraries, such as https://github.com/brotoo25/firestore-coroutines.
m
Sample of AWS login with 2 different callbacks