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

Dalinar

02/04/2019, 11:52 AM
I want to replace the
GlobalScope
in `GlobalScope.async { }`but without using
coroutineScope { }
- I mean I want to write
x.async{ }
- how do I do that? what should
x
be? to make it the equivalant of doing
coroutineScope { }
? is this possible? the reason I ask is because it requires a lot of reworking to use
coroutineScope { }
for this section of code
g

gildor

02/04/2019, 12:05 PM
I want to replace the
GlobalScope
in `GlobalScope.async { }`but without using
coroutineScope { }
What do you mean? coroutineScope has very different semantics and use case
I mean I want to write
x.async{ }
- how do I do that? what should
x
be?
It should be some CoroutineScope.
d

Dalinar

02/04/2019, 12:06 PM
how do I make a CoroutineScope without the
coroutineScope { }
construct ?
g

gildor

02/04/2019, 12:06 PM
implement CoroutineScope
d

Dalinar

02/04/2019, 12:07 PM
ok I'll look into how to implement CoroutineScope, thanks
g

gildor

02/04/2019, 12:07 PM
Usually coroutine scope is just some class that has lifecycle: UI component, connection etc
What is your use case? Where exactly you want call
x.async{}
?
d

Dalinar

02/04/2019, 12:07 PM
hang on
my current code is like this
Copy code
val f: Deferred<Status> = abc.deferredClose ?: let {
                val g = GlobalScope.async { foo(abc) } 
                abc.deferredClose = g
                g
            }
the GlobalScope is there because I'm upgrading from older coroutines and had to do something temporarily to get it to compile
g

gildor

02/04/2019, 12:21 PM
If it’s migration step than GlobalScope is just fine
d

Dalinar

02/04/2019, 12:22 PM
ok I'll leave it as is then for now, thanks
g

gildor

02/04/2019, 12:22 PM
If you want migrate to StructuredConcurrency, class that contains
f
should implement CoroutineScope and you should control it lifecycle (otherwise you leaking coroutine)
d

Dalinar

02/04/2019, 12:24 PM
ok, I'll make note of this and will do that at a future date
7 Views