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

gildor

10/09/2018, 5:21 AM
But as standalone coroutines are deprecated what other alternatives do we have apart from GlobalScope
What do you mean? GlobalScope is direct successor of old standalone coroutine builders and provides the same lifecycle. But usually you don’t need it, use CoroutineScope everywhere where it’s possible to do
has examples about
runBlocking{ }
and
GlobalScope.launch( )
only
It’s just for simplicity of examples, docs has actually a lot of different examples, not only runBlocking or GlobalScope
alternatives for CoroutineContext builders? What do you mean?
r

Rohit Surwase

10/09/2018, 6:53 AM
Like as GlobalScope is application wide, what if I want to create a Coroutines context specifically for a dialog.
g

gildor

10/09/2018, 6:53 AM
Okay, first of all it’s not a Coroutine context
CoroutineContext is like metadata for coroutine used to provide configs to coroutines
If you have some custom lifecycle you have 2 options: 1. Create CoroutineScope just for this lifecycle and run coroutine there + UPD You should of course do not forget to cancel scope when your dialog cancelled 2. Create a new coroutine in parent level lifecycle but cancel it manually (start coroutine, get Job instance and cancel this job on dialog cancel)
case with custom CoroutineScope make sense for me if you run multiple coroutines there, otherwise one single Job for one single coroutine looks fine for me
If you talking about Android you shouldn’t use GlobalScope (only if you run something on Application level), only scopes related to Activity/Fragment lifecycle
r

Rohit Surwase

10/09/2018, 7:00 AM
Thanks so much, all this information is so useful for me.
3 Views