https://kotlinlang.org logo
Title
l

Laura de la rosa

08/29/2019, 2:37 PM
Hi!, all... which is the difference between GlobalScope.async and CoroutineScope(context).launch?? can someone explain me
d

Dominaezzz

08/29/2019, 2:38 PM
There are 4 differences here.
👀 1
launch
vs
async
.
GlobalScope
vs
CoroutineScope(context)
The missing differences are in that hidden
context
.
l

Laura de la rosa

08/29/2019, 2:40 PM
I need the difference between this
GlobalScope
vs
CoroutineScope(context)
d

Dominaezzz

08/29/2019, 2:42 PM
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

Laura de la rosa

08/29/2019, 2:46 PM
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

streetsofboston

08/29/2019, 2:47 PM
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

Laura de la rosa

08/29/2019, 2:47 PM
because I never specify a context for ios
s

streetsofboston

08/29/2019, 2:48 PM
When doing
val scope = CoroutineScope()
, it will add a
Job()
to its coroutineContext, by default
d

Dominaezzz

08/29/2019, 2:49 PM
Which won't matter if you don't keep a reference to it.
s

streetsofboston

08/29/2019, 2:50 PM
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

Laura de la rosa

08/29/2019, 2:52 PM
so I should use a val scope = CoroutineScope(context)
s

streetsofboston

08/29/2019, 2:52 PM
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

Laura de la rosa

08/29/2019, 2:53 PM
and if I dont want a scope.cancel()
I can use GlobalScope
:yes: 2
thank you very much ❤️
s

streetsofboston

08/29/2019, 2:54 PM
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

Laura de la rosa

08/29/2019, 2:55 PM
how I provide a dispatcher in
GlobalScope
?
s

streetsofboston

08/29/2019, 2:55 PM
GlobalScope.launch(dispatcher) { … }
or
GlobalScope.launch {
    withContext(dispatcher) {
        ... 
    }
}
❤️ 1
l

Laura de la rosa

08/29/2019, 2:57 PM
thank you very much! you both are the best ❤️
s

streetsofboston

08/29/2019, 2:57 PM
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

Laura de la rosa

08/29/2019, 2:58 PM
why CoroutineScope(dispatcher) blocks me the app and GlobalScope no?
s

streetsofboston

08/29/2019, 2:59 PM
Not sure what you mean with ‘blocks me the app’.
l

Laura de la rosa

08/29/2019, 3:00 PM
is like CoroutineScope(dispatcher) no has a async behaviour
s

streetsofboston

08/29/2019, 3:00 PM
You said you use it on iOS?
:yes: 1
d

Dominaezzz

08/29/2019, 3:01 PM
Oh that's why.
s

streetsofboston

08/29/2019, 3:01 PM
Google “kotlin ios coroutinescope” and you’ll find quite a few articles/post/issues with this (and hopefully some solutions)
✔️ 1
z

Zach Klippenstein (he/him) [MOD]

08/29/2019, 3:25 PM
@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

basher

08/29/2019, 3:31 PM
Slides are up! No video yet: https://twitter.com/benasher44/status/1166892473303494657?s=21
🤘🏼 2
❤️ 2
🎉 3