https://kotlinlang.org logo
Title
a

ar-g

05/07/2020, 1:03 PM
Hey folks, I've got another question regarding exception handling with Flow. Current implementation throws
Flow invariant is violated: Emission from another coroutine detected...
It makes sense, the question is how to not violate it and ensure that: • Request and observing DB happens in parallel • Exception wrapped and propagated to UI • UI continues to observe DB
fun getAll(): Flow<Wrap<...>> = flow { //request and observing db happens in parallel
    coroutineScope {
        launch(<http://Dispatchers.IO|Dispatchers.IO>) {
            try {
                //start request...
            } catch (e){
                emit(Wrap(e))//!!! this will throw IllegalStateException: Flow invariant is violated: Emission from another coroutine is detected.
            }
        }
        emitAll(db.flow()) //starts emitting immediately...
    }
}
d

Dominaezzz

05/07/2020, 1:41 PM
Use a
channelFlow
.
a

ar-g

05/08/2020, 6:53 AM
Thanks Dominic, works perfectly for this use-case. I was about to contribute to the official doc, but then noticed that
channelFlow
is still
experimental
.