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

Mikołaj Kąkol

03/19/2020, 7:14 AM
hi, if I'm given a
scope
called
bigScope
how can I create a scope
smallScope
that will be canceled when
bigScope
is cancelled?
o

octylFractal

03/19/2020, 7:15 AM
CoroutineScope(Job(bigScope.coroutineContext[Job]))
?
m

Mikołaj Kąkol

03/19/2020, 7:18 AM
Copy code
val smallScope = CoroutineScope(Dispatchers.Main + bigScope.coroutineContext)
like that?
is Job necessary?
Copy code
val smallScope = CoroutineScope(Dispatchers.Main + Job(bigScope.coroutineContext[Job]))
compiles too, could you point a difference?
o

octylFractal

03/19/2020, 7:19 AM
yes, the
Job(parent: Job)
constructor means structured concurrency
basically it declares that
smallScope
is a child of
bigScope
rather than the same Job as
bigScope
m

Mikołaj Kąkol

03/19/2020, 7:20 AM
great, thankyou 🙂
2 Views