Hello. I have a flow created with `channelFlow`, a...
# coroutines
y
Hello. I have a flow created with
channelFlow
, and a simple flow created with
flowOf
. I want to concatenate them so I do
val concatened = chanFlow.onCompletion { if(it==null) emitAll(simpleFlow) }
. Then the flow is "launched"
GlobalScope.launch { concatened.collect { ... } }
. When I cancel the resulting job, the chanFlow stop correctly and throw
CancellationException
, but the second flow values are still collected. The
throwable
in
onCompletion
is null. What is the correct way to handle this ?
p
Can you post a self contained example?
y
v
It’s because cancellation is not an upstream exception (it’s also mentioned in
onCompletion
documentation). You can use
if (it == null && coroutineContext.isActive)
to ignore cancellation as well
y
Ok, thanks you. (I saw the line in the doc about cancellation exception, but did not see how to achieve what I wanted and if I was on the right track).