gotoOla
02/04/2020, 2:57 PMzak.taccardi
02/04/2020, 2:58 PMCoroutineContext
- why are you doing it?diesieben07
02/04/2020, 3:00 PMCoroutineScope
with CoroutineContext
? A CoroutineScope
has "children" and has "wait for children" built-in. CoroutineContext
can contain arbitrary elements, such as Job
.Job
or maybe a SupervisorJob
for your "container" which you can then wait forAdam Powell
02/04/2020, 3:04 PMcoroutineScope {
// launch/do work here
}
// all children joined by here
gotoOla
02/04/2020, 4:52 PMdiesieben07
02/04/2020, 4:57 PMCoroutineScope
it (usually) has an associated Job in it's associated CoroutineContext
(see docs on CoroutineScope.coroutineContext
). So:
checkNotNull(myScope.coroutineContext[Job]) { "CoroutineScope is missing Job" }.join()
gotoOla
02/04/2020, 4:58 PMfun stop() {
isRunning = false
/* perform the "stop action on coroutinescope
so that the consumers that are active finishes /*
}
diesieben07
02/04/2020, 4:59 PMCoroutineScope
manually you might have to create your Job manually as well, depending on your usage.SupervisorJob
is actually a factory function which produces a special kind of parent-job (see the docs for details).gotoOla
02/04/2020, 5:03 PMclass MyClass: CoroutineScope by CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>)
processors.addAll(
(1..maxNumberOfMessagesInParallel).map {
<http://logger.info|logger.info>("Starting processing channel.")
launchMessageProcessor()
}
)
with processor as a class field and then in my stop function I do
fun stop() {
isRunning = false
processors.joinAll()
}
diesieben07
02/04/2020, 5:04 PMCoroutineScope
factory function automatically introduces a Job
into the CoroutineContext
if one is not already presentgotoOla
02/04/2020, 5:06 PMthis.coroutineContext
it doesn't seem to hold a reference to any jobdiesieben07
02/04/2020, 5:06 PMContextScope(if (context[Job] != null) context else context + Job())
Job
.gotoOla
02/04/2020, 5:08 PMdekans
02/04/2020, 5:13 PMcoroutineContext[Job]?.children?.forEach { it.cancelAndJoin() }
Right before cancelling the scope