What is the difference between GlobalScope.launch(...
# coroutines
a
What is the difference between GlobalScope.launch(Dispatchers.IO) {} And CoroutineScope(Dispatchers.IO).launch{} I have read from this StackOverflow https://stackoverflow.com/questions/65008486/globalscope-vs-coroutinescope-vs-lifecyclescope that both are same except the syntax differs but there has to be some difference so that we can use each one accordingly.
d
I think, the answers there are great. Take a look at this one with title “TL;DR”
j
You should honestly never use the second approach. Creating a custom
CoroutineScope
goes with controlling its lifecycle. If you create one, you should store it in a variable and cancel it appropriately depending on the lifecycle you want to give it. Otherwise, it's just as bad as
GlobalScope
.
1
a
@Joffrey make sense thanks
u
And Scopes also get garbage collected when not referenced