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

janvladimirmostert

07/04/2022, 10:05 PM
Should supervisorScope be inside launch or outside launch or doesn't it matter?
Copy code
supervisorScope {
   launch(Dispatchers.Default) {
      supervisorScope {
j

Joffrey

07/05/2022, 9:23 AM
What are you trying to achieve exactly? If
launch
is supposed to be launched in a supervisor scope to avoid cancelling its sibling coroutines when it fails, then it should be inside the
supervisorScope
lambda.
j

janvladimirmostert

07/05/2022, 11:19 AM
Each launch is triggered when a connection happens, so any exception being thrown inside one of these launches should not cancel the other launches being triggered by other connections. Running launch inside supervisorScope sounds like the correct way, but the mental model I have of supervisorScope makes me think it'll work inside launch too.
j

Joffrey

07/05/2022, 12:25 PM
The scope's purpose is to control a set of coroutines. So all the launches that you want to "group" together should be launched in the same supervisor scope, otherwise there is no real point.
👍 1
🙏 1
9 Views