Hey folks, I've got another question regarding exc...
# coroutines
a
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
Copy code
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
Use a
channelFlow
.
a
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
.