Osman Saral
07/27/2023, 2:48 PMflow {}
block form another function? What I'm trying to do is this:
private val scope = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>)
private val exceptionFlow = MutableSharedFlow<Throwable>()
fun initFlow(scope: CoroutineScope) = flow {
scope.launch {
exceptionFlow.collect {
print("throwing")
throw it //throw here to trigger retry
}
}
emit(1)
delay(2000L)
emit(2)
}.retry(2) { cause ->
println("retrying cause: $cause")
true
}.catch {
println("caught $it")
emit(3)
}
suspend fun retry(throwable: Throwable) {
exceptionFlow.emit(throwable)
}
fun main() {
runBlocking {
initFlow(scope).collect {
println(it)
}
delay(500L)
retry(IllegalStateException("error"))
}
}
This prints
1
2
throwing
the exception is not caught and the app doesn't crash. Why doesn't it work and what's the best way to implement something like this?franztesca
07/27/2023, 3:55 PMscope
) and not on the flow of initFlow. Use coroutineScope instead
fun initFlow(scope: CoroutineScope) = flow {
coroutineScope {
launch {
exceptionFlow.collect {
print("throwing")
throw it //throw here to trigger retry
}
}
emit(1)
delay(2000L)
emit(2)
}
}
aarkling
07/27/2023, 8:51 PMOsman Saral
07/28/2023, 9:38 AMEvaluation stopped while it's taking too long️
error on kotlin playground. It hangs somewhere.
Here is my playground link https://pl.kotl.in/_Ye9vwIhPfranztesca
07/28/2023, 9:43 AMOsman Saral
07/28/2023, 10:55 AMOsman Saral
07/31/2023, 2:58 PM