Christian Ekrem
02/01/2021, 1:16 PM// stream is _really_ someDao.loadAll(): Flow<Something>
val stream = MutableStateFlow("cachedValue")
// refresh is _really_ fetchSomethingFromApi.also { someDao.updateData(it) }
val refresh = suspend {
delay(1000)
stream.value = "freshValueFromAPI"
}
suspend fun whyDoesThisNotWork(): Flow<String> = stream
.onStart {
coroutineScope {
launch {
refresh()
}
}
}
suspend fun thisWorks(): Flow<String> = flow {
coroutineScope {
launch {
refresh()
}
stream.collect {
emit(it)
}
}
}
It’s not like the working version is horrible, I just want to understand why the first one does not work!runBlocking {
thisWorks().take(2).collect {
println(it)
}
}
and
runBlocking {
whyDoesThisNotWork().take(2).collect {
println(it)
}
}
The working one emits what I expect, when I expect (that is “cachedValue” immediately, and then “freshValueFromAPI” after one second). The other one simply times out. If I do .take(1)
instead, I find that the broken version only ever collects “freshValueFromAPI”