https://kotlinlang.org logo
#coroutines
Title
# coroutines
y

ylemoigne

09/30/2019, 7:46 AM
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

Paul Woitaschek

09/30/2019, 7:58 AM
Can you post a self contained example?
y

ylemoigne

09/30/2019, 8:02 AM
v

Vsevolod Tolstopyatov [JB]

09/30/2019, 9:56 AM
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

ylemoigne

09/30/2019, 10:56 AM
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).
4 Views