[英]Kotlin: Override parent job of coroutine
I am trying to migrate the following function to new Coroutine
of Kotlin 1.3
: 我正在尝试将以下功能迁移到新的Kotlin 1.3
Coroutine
:
fun launchUI(strategy: CancelStrategy, block: suspend CoroutineScope.() -> Unit): Job {
return launch(context = UI, parent = strategy.jobs, block = block)
}
But new GlobalScope.launch
function doesn't have parent
parameter. 但是新的GlobalScope.launch
函数没有parent
参数。 Documentation says: 文档说:
The parent job is inherited from a
CoroutineScope
as well, but it can also be overridden with correspondingcoroutineContext
element. 父作业也继承自CoroutineScope
,但也可以用相应的coroutineContext
元素覆盖。
But I don't know how to override parent job. 但是我不知道该如何替代上级工作。 I have implemented it like this for now but I am not sure if it will work the same way: 我现在已经像这样实现了,但是我不确定它是否可以相同的方式工作:
fun launchUI(strategy: CancelStrategy, block: suspend CoroutineScope.() -> Unit): Job {
val job = GlobalScope.launch(context = Dispatchers.Main, block = block)
strategy.jobs.invokeOnCompletion {
job.cancel()
}
return job
}
Can anyone help me? 谁能帮我?
UPDATE: 更新:
class CancelStrategy(owner: LifecycleOwner, val jobs: Job) : LifecycleObserver {
init {
owner.lifecycle.addObserver(this)
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
jobs.cancel()
}
}
Your second example is correct. 您的第二个例子是正确的。 You can use plus
to add the job as a parent job for the new coroutine. 您可以使用plus
将该作业添加为新协程的父作业。
fun launchUI(strategy: CancelStrategy, block: suspend CoroutineScope.() -> Unit): Job {
return GlobalScope.launch(context = Dispatchers.Main + strategy.jobs, block = block)
}
But the usage of GlobalScope
is discouraged. 但是不鼓励使用GlobalScope
。 It would be better to create an own CoroutineScope
. 最好创建一个自己的CoroutineScope
。 Your CancelStrategy
looks like a good candidate. 您的CancelStrategy
看起来不错。
class CancelStrategy(owner: LifecycleOwner, val jobs: Job) : LifecycleObserver, CoroutineScope {
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + jobs
init {
owner.lifecycle.addObserver(this)
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
jobs.cancel()
}
}
Now you can start your coroutines like this: 现在,您可以像下面这样启动协程:
cancelStrategy.launch { ... }
What you want to have is called "structured concurrency" by aligning the lifecycle of your coroutines to some UI component. 通过将协程的生命周期与某些UI组件对齐,可以将其称为“结构化并发”。
Have a look at this documentation: https://github.com/Kotlin/kotlinx.coroutines/blob/master/ui/coroutines-guide-ui.md#structured-concurrency-lifecycle-and-coroutine-parent-child-hierarchy 看看这个文档: https : //github.com/Kotlin/kotlinx.coroutines/blob/master/ui/coroutines-guide-ui.md#structured-concurrency-lifecycle-and-coroutine-parent-child-hierarchy
Instead of using GlobalScope
, you should consider implementing your own scope and maintaining a Job
their, which you can cancel to cancel all your children as well. 而不是使用GlobalScope
,您应该考虑实现自己的范围并维护Job
它们,您也可以取消它来取消所有子级。
Here's a simplified example: 这是一个简化的示例:
class Activity : CoroutineScope {
lateinit var job: Job //tied to lifecycle of Activity
fun create() {
job = Job()
}
fun destroy() {
//will cancel all child jobs as well
println("cancel $job and all ${job.children.toList().size} children")
job.cancel()
}
override val coroutineContext: CoroutineContext
get() = Dispatchers.Default + job + CoroutineName("MyActivityContext")
fun doSomething() {
//we launch in the outer scope of Activity
launch {
//...
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.