I want to replace the `GlobalScope` in `GlobalScop...
# coroutines
d
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
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
how do I make a CoroutineScope without the
coroutineScope { }
construct ?
g
implement CoroutineScope
d
ok I'll look into how to implement CoroutineScope, thanks
g
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
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
If it’s migration step than GlobalScope is just fine
d
ok I'll leave it as is then for now, thanks
g
If you want migrate to StructuredConcurrency, class that contains
f
should implement CoroutineScope and you should control it lifecycle (otherwise you leaking coroutine)
d
ok, I'll make note of this and will do that at a future date