William Reed
04/19/2022, 6:37 PMCoroutineScope
which has a long lifecycle. I want to make another scope which has a shorter lifecycle (potentially several in the same span as the 'parent'). If the parent scope is cancelled I want both parent and the shorter lifecycle scope to be cancelled
as far as i can tell you can't inherit scopes like this. another (seemingly bad) idea I have is to maintain a collection of `Job`s for the shorter lifecycle group, which will allow me to cancel everything in that collection, or if the parent scope is cancelled, they would all be launched under it anyway, and get cancelledJoffrey
04/19/2022, 8:38 PMas far as I can tell you can't inherit scopes like this
You can create such a child scope by creating it with an explicit job that has the job from the parent scope as parent:
CoroutineScope(Job(parent = parentScope.job))
(I think there is a .job
extension property like this on the coroutine scope)Joffrey
04/19/2022, 8:41 PMparentScope.launch { // this = child scope
...
}
Zach Klippenstein (he/him) [MOD]
04/19/2022, 9:19 PMWilliam Reed
04/19/2022, 9:41 PMWilliam Reed
04/19/2022, 9:43 PMparentScope.coroutineContext.job
William Reed
04/19/2022, 9:54 PMlaunch
example is obvious with normal structured concurrency. didn't realize i could do this to jobs thoughJoffrey
04/19/2022, 9:56 PMWilliam Reed
04/19/2022, 9:59 PM