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

Dias

02/28/2019, 12:41 PM
Hi, let's say I have 3 coroutines: A,B and C. I want A and B be related, as in when A fails B cancels as well, and vice-a-versa. I also want C depend on A and B, but only one direction, so A and B failure would cancel C, but not other way around. There is probably simple way to do it, but I am confused now. I know of supervisor scope, but that makes all children equally unrelated, while I have some sort of downward hierarchy: PARENT / \ A - B \ / C
g

gildor

02/28/2019, 1:15 PM
Maybe you could provide some examples how those coroutines are interact, otherwise it's not very clear Looks that you just can run a and b in one coroutineScope and from parent
d

Dias

02/28/2019, 2:08 PM
they don't really interact, just want to manage failures better. Let me show you what I do currently:
Copy code
suspend fun callMany() = coroutineScope {
      launch { callA() }
      launch { callB() }
      
      CoroutineScope(this.coroutineContext + Job()).launch {
                callC()
      }
}
this way when callA() and callB() fail, they make callMany() throw an error and cancel each other, but callC() continues. I want callC() to be cancelled when callA() or callB() fails, but if callC() fails it woudn't affect callA() or callB(). While preserving the rest of functionality
2 Views