Darren Gu
07/30/2020, 7:00 PMfun main() = runBlocking {
test()
}
suspend fun test() {
val job = coroutineScope {
launch {
delay(5000)
}
}
println("job = $job")
}
I need to have a reference to the job when it’s running. But the print doesn’t seem to execute until 5 sec later. What would be the correct way to achieve this?fatih
07/30/2020, 7:02 PMDarren Gu
07/30/2020, 7:06 PMfatih
07/30/2020, 7:08 PMDarren Gu
07/30/2020, 7:08 PMTijl
07/30/2020, 7:10 PMcoroutineScope
suspends until all child jobs finish, this explains your delay.
inside coroutineScope
you have this
Darren Gu
07/30/2020, 7:12 PMTijl
07/30/2020, 7:15 PMCoroutineScope
? inside coroutineScope
this
refers to the scope which also consists of the Job AFAIKoctylFractal
07/30/2020, 7:16 PMcoroutineScope {
val job = launch { delay(5000) }
println("job = $job")
}
Tijl
07/30/2020, 7:17 PMcoroutineScope { this.coroutineContext }
also contains a job AFAIKDarren Gu
07/30/2020, 7:24 PMoctylFractal
07/30/2020, 7:28 PMcoroutineContext
available inside themDarren Gu
07/30/2020, 7:31 PMoctylFractal
07/30/2020, 7:33 PMCoroutineScope(coroutineContext)
-- no, coroutineScope
also opens a new job, and if the current coroutineContext
has a Job
, the new one is a child of itDarren Gu
07/30/2020, 7:35 PMoctylFractal
07/30/2020, 7:40 PMDarren Gu
07/30/2020, 7:41 PM