uhe
10/01/2018, 3:35 PM@Deprecated("No replacement. Group child in a coroutineScope { } block to wait for them")
public suspend fun Job.joinChildren()
However, I don't see how to "group child in a coroutineScope".
For example:
val networkContext = <http://Dispatchers.IO|Dispatchers.IO>
val networkJob = Job()
private fun onNetworkContext(block: suspend CoroutineScope.() -> Unit) =
GlobalScope.launch(networkContext + networkJob, block = block)
// invoked from another coroutine:
suspend fun abortAndRetry() {
networkJob.cancelChildren()
// how do I replace this properly?
networkJob.joinChildren()
// retry ...
}
Jonathan
10/01/2018, 3:39 PMvar networkScope = CoroutineScope(Job() + <http://Dispatchers.IO|Dispatchers.IO>)
private fun onNetworkContext(block: suspend CoroutineScope.() -> Unit) =
networkScope.launch(block = block)
// invoked from another coroutine:
suspend fun abortAndRetry() {
networkScope.coroutineContext[Job]?.cancelAndJoin()
// retry ...
networkScope = CoroutineScope(Job() + <http://Dispatchers.IO|Dispatchers.IO>)
}
GlobalScope
if you want to manage the life cycle of child jobs.elizarov
10/01/2018, 3:40 PMonNetworkContext
and abortAndRetry
? I would be grateful if you create an issue in <http://github.com/kotlin/kotlinx.coroutines/issues>
so that we can have a discussion here.joinChildren
, but let’s see if the use-case is strong enough.networkJob.children().forEach { it.join() }
cancelChildren
and joinChildren
. So, I strongly suggest to rewrite this code via networkJob.cancelAndJoin
and then create a new (non-cancelled) parent job for all further childrenuhe
10/01/2018, 3:43 PMelizarov
10/01/2018, 3:43 PMjoinChildren
in inherently racyuhe
10/01/2018, 3:44 PM