Hi!, all... which is the difference between Global...
# coroutines
l
Hi!, all... which is the difference between GlobalScope.async and CoroutineScope(context).launch?? can someone explain me
d
There are 4 differences here.
👀 1
launch
vs
async
.
GlobalScope
vs
CoroutineScope(context)
The missing differences are in that hidden
context
.
l
I need the difference between this
GlobalScope
vs
CoroutineScope(context)
d
The latter adds an extra allocation. Behaviour is about the same.
I can give a more in-depth explanation, if I know what is in
context
.
l
ok, I need to access to the contacts of the cellphone, I need this operation async, if I try using CoroutineScope(context) my app blocks, if I try with GlobalScope.launch I get what I want. but Im worjing in kotlin multiplatform so I need this working for android and ios... in android always take the main context by default, but in ios I need to specify it. so I don't know if GlobalScope.launch is gonna work for ios
s
For `GlobalScope`: It has an ‘empty’ context, ie. no fixed dispatcher (launches, asyncs use the Dispatchers.Unconfined, unless you switch it by using
withContext
, etc), and it has no
Job
(ie. You can’t cancel a
GlobalScope
, for example)
👍 1
💯 1
l
because I never specify a context for ios
s
When doing
val scope = CoroutineScope()
, it will add a
Job()
to its coroutineContext, by default
d
Which won't matter if you don't keep a reference to it.
s
Yup, true. But you may want to provide a
SupervisorJob()
if needed. But the default providing of
Job()
is a good default. But using the factory method
CoroutineScope(...)
, it always will have a Job, which means it is cancelable, whereas GlobalScope isn’t
✔️ 1
l
so I should use a val scope = CoroutineScope(context)
s
If you are planning on calling
scope.cancel()
at some point, yes.
☝🏼 1
If you launches and asyncs are truly global and should never be canceled (unless you entire process aborts), then use GlobalScope.
l
and if I dont want a scope.cancel()
I can use GlobalScope
👌 2
thank you very much ❤️
s
Looks like it. Be sure to provide the proper dispatcher to your launches and asyncs, since GlobalScope uses the Unconfined dispatcher, which may surprise you here and there 🙂
☝🏼 1
l
how I provide a dispatcher in
GlobalScope
?
s
GlobalScope.launch(dispatcher) { … }
or
Copy code
GlobalScope.launch {
    withContext(dispatcher) {
        ... 
    }
}
❤️ 1
l
thank you very much! you both are the best ❤️
s
I must admit, to avoid providing the dispatcher when calling launch and such, instead of GlobalScope, I use CoroutineScope(dispatcher) …. I just never call ‘cancel()’ on it 🙂
😮 1
l
why CoroutineScope(dispatcher) blocks me the app and GlobalScope no?
s
Not sure what you mean with ‘blocks me the app’.
l
is like CoroutineScope(dispatcher) no has a async behaviour
s
You said you use it on iOS?
👌 1
d
Oh that's why.
s
Google “kotlin ios coroutinescope” and you’ll find quite a few articles/post/issues with this (and hopefully some solutions)
✔️ 1
z
@basher just gave a great talk on using coroutines in K/N to do background work – not sure if the slides are up anywhere
❤️ 1
b
🤘🏼 2
❤️ 2
🎉 3