I have the following use case:
My function returns a flow.. for simplicity lets say it returns
Flow<Int>
In this function I have an api call which also returns a flow
What is the best way to give result back to the caller?
Sample code in thread
Ruben Quadros
10/24/2022, 7:20 AM
Copy code
fun apiCall(): Flow<Int> = flow {
delay(2000) // some network call
emit(3)
}
Copy code
fun myFunc(): Flow<Int> = flow {
emit(1) //denote loading
apiCall()
//is the only way collecting here and emitting again?
// apiCall().collect { emit(it) }
}
call site
Copy code
myFunc().collect {
//do something with each result
}
j
Joffrey
10/24/2022, 7:29 AM
Why does
apiCall
return a flow of one element? One-shot calls are way easier to manipulate as simple suspend functions
Joffrey
10/24/2022, 7:31 AM
If you really really want a flow from the API call, you could use
emitAll
I guess, but I would really reconsider the
apiCall
function first
r
Ruben Quadros
10/24/2022, 7:32 AM
understood.. making it a one-shot call will be better 👍