How can I emit the result to the consumer from a different function.
When I try the following nothing happens?
Copy code
fun fetchInfo(): Flow<State<SportInfo>> =
flow {
val result = memoryInfoResult
if (result != null) {
emit(result)
} else {
fetchSportInfo()
.onEach { result ->
when (result) {
is Data -> {
memoryInfoResult = result
emit(result)
}
is Error, is Loading -> {
emit(result)
}
}
}
}
}
n
Nick Allen
12/07/2021, 7:55 AM
Replace
onEach
with
collect
.
onEach
is not a terminating operator (not one that actually runs the flow).
👍 1
Nick Allen
12/07/2021, 8:02 AM
Also, this code is setup so if you have a cached result, you will never return anything else (updates are in an else).
Nick Allen
12/07/2021, 8:14 AM
And remember that
Flow
is cold. So if you have 5 collectors, then they are all updating
memoryInfoResult
. Usually for something with shared data, I prefer a