Can someone explain me why the second child is not...
# coroutines
l
Can someone explain me why the second child is not cancelled?
Copy code
coroutineScope {
		launch {
			if (condition) cancel() // does not cancel the coroutine of flow
		}
		
		launch {
			dataSource.channel.receiveAsFlow() {
				.collect { doSomething() }
		}
	}
I would expect that cancelling first coroutine would cancel second coroutine but it doesn't. Any ideas?
l
It'd if it crashed (i.e. you throw a non
CancellationException
), but I guess what you're looking for here is doing
this@coroutineScope.cancel()
.
a
The reason is that
launch
will create a new
CoroutineScope
for the job which is different from the outer scope. You are canceling the inner scope so it won't affect other jobs in the outer scope.
💯 2
l
@Albert Chang hmm but
coroutineScope
states: "When any child coroutine in this scope fails, this scope fails and all the rest of the children are cancelled". My example macthes this statement exactly, not?
a
@Lilly "fails" means throwing exception other than
CancellationException
.
l
Ah oookk, got it!
l
Yep, fails is what I called "crashed"
l
Nice ok, thanks a lot guys ❤️
blob smile 1