【发布时间】:2020-07-09 14:05:11
【问题描述】:
我正在尝试使用 Kotlin 协程而不是传统 Java 线程来执行后台操作:
我从这个link 中学到了,它工作正常
val job = launch {
for(file in files) {
ensureActive() //will throw cancelled exception to interrupt the execution
readFile(file)
}
}
但我的情况是我有一个非常复杂的 readFile() 调用函数,我如何检查该函数中的作业是否处于活动状态?
val job = launch {
for(file in files) {
ensureActive() //will throw cancelled exception to interrupt the execution
complexFunOfReadingFile(file) //may process each line of the file
}
}
我不想在这个协程范围内复制函数的 impl 或将作业实例作为参数传递给该函数。 官方的处理方式是什么?
非常感谢。
【问题讨论】:
标签: android kotlin kotlin-coroutines