Is there any way to launch onto different coroutin...
# coroutines
w
Is there any way to launch onto different coroutinescopes inside a for loop so that every iteration of the for loop still doesn't block each other?
for example
Copy code
for (function in listOfSuspendingFunctions) {
  launch {
    function.call()
  }
}
I don't want each instance of
function.call()
to block
c
.async { }
is the builder you’d want to use instead of
.launch { }
w
oh ok I will try that
d
What's wrong with your initial example though? That looks fine.
w
that's what I thought but it is still blocking even though it didn't block in my testing
d
Interesting. By block you mean suspend right?
w
yes
well i figured it out, i just added a globalScope.launch inside the function being called instead of wrapping the function call inside a launch { } and it just works
idk y
but thanks for all the help guys!
m
You shouldn't really use GlobalScope though.
c
Launching into an unrelated scope will break “structured concurrency” and in general is not a recommended practice. It’s probably some issue with the combnation of where you
launch
, where you have inner
coroutineScope { }
blocks, and maybe the initial scope you’re starting from. Can you paste the whole snippet from your code here (and not just an example)?
w
sorry for not getting back to you guys yes, unfortunately I understand launching into globalScope isn’t the best practice but this code was for my robotics team and we needed a solution quick for a virtual competition (we work with android so that’s why we use kotlin) I am not able to paste the whole snippet as not only is it multiple files within our code base, it would also require some knowledge about the given SDK and API already set up and I don’t believe you guys would want to spend time trying to understand any of that for a simple question
So I think I will just stick with this solution as of right now but will continue trying other possible solutions and ask more questions if needed. thanks for all your input and help!