android - Kotlin CoroutineScope 无法在 android View 中取消

标签 android kotlin coroutine kotlin-coroutines

例如,这个 View 。当 onDetachedFromWindow 调用范围被取消时,但启动的作业仍然处于 Activity 状态。

class TestView : FrameLayout,CoroutineScope {
    val TAG = "TestView"
    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + Job()

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        launch {
            while (true) {
                Log.i(TAG,"is in launch coroutine....${coroutineContext} ${this@TestView.coroutineContext}")
                delay(1000)
            }
        }
    }
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        cancel()
        Log.i(TAG,"onDetachedFromWindow")
    }
}

日志是

2019-09-19 21:32:26.652 22912-22912/com.ymr.myapplication I/TestView: is in launch coroutine....[StandaloneCoroutine{Active}@7f9d20f, Main] [JobImpl{Active}@2f3fde2, Main]
2019-09-19 21:32:27.655 22912-22912/com.ymr.myapplication I/TestView: is in launch coroutine....[StandaloneCoroutine{Active}@7f9d20f, Main] [JobImpl{Active}@80a2573, Main]
2019-09-19 21:32:28.656 22912-22912/com.ymr.myapplication I/TestView: is in launch coroutine....[StandaloneCoroutine{Active}@7f9d20f, Main] [JobImpl{Active}@dad2c30, Main]
2019-09-19 21:32:29.649 22912-22912/com.ymr.myapplication I/TestView: onDetachedFromWindow
2019-09-19 21:32:29.665 22912-22912/com.ymr.myapplication I/TestView: is in launch coroutine....[StandaloneCoroutine{Active}@7f9d20f, Main] [JobImpl{Active}@dab39f4, Main]
2019-09-19 21:32:30.666 22912-22912/com.ymr.myapplication I/TestView: is in launch coroutine....[StandaloneCoroutine{Active}@7f9d20f, Main] [JobImpl{Active}@448351d, Main]
2019-09-19 21:32:31.668 22912-22912/com.ymr.myapplication I/TestView: is in launch coroutine....[StandaloneCoroutine{Active}@7f9d20f, Main] [JobImpl{Active}@45ba392, Main]
2019-09-19 21:32:32.669 22912-22912/com.ymr.myapplication I/TestView: is in launch coroutine....[StandaloneCoroutine{Active}@7f9d20f, Main] [JobImpl{Active}@bc75163, Main]

那么为什么不能取消作用域启动的作业呢?

最佳答案

问题是您每次都在 coroutineContext 中创建一个新的 Job。您可以使用

轻松解决此问题
override val coroutineContext = Dispatchers.Main + Job()

但是,请记住,如果您的 View 在分离后附加到窗口,则 coroutineContext 将已被取消。

请查看以下示例,以更好地了解它的工作原理:

class MyActivity : AppCompatActivity(), CoroutineScope {
    lateinit var job: Job
    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + job

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        job = Job()
    }

    override fun onDestroy() {
        super.onDestroy()
        job.cancel() // Cancel job on activity destroy. After destroy all children jobs will be cancelled automatically
    }

    /*
     * Note how coroutine builders are scoped: if activity is destroyed or any of the launched coroutines
     * in this method throws an exception, then all nested coroutines are cancelled.
     */
    fun loadDataFromUI() = launch { // <- extension on current activity, launched in the main thread
       val ioData = async(Dispatchers.IO) { // <- extension on launch scope, launched in IO dispatcher
           // blocking I/O operation
       }
       // do something else concurrently with I/O
       val data = ioData.await() // wait for result of I/O
       draw(data) // can draw in the main thread
    }
}

关于android - Kotlin CoroutineScope 无法在 android View 中取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58022726/

相关文章:

android - 如何在 viewModel Kotlin Android 中保护安全?

python - 如何在同步环境中执行 Tornado 协程?

android - 我必须使用哪个功能才能使我的应用程序仅适用于包含 Orientation Sensor 的设备?

android - 在 Android API 版本 > 6.0 (API 23) 中未经用户许可发送短信

Android:带有 Butterknife 的 Kotlin

kotlin - 比较 Kotlin 中没有 ID 的数据类

multithreading - 在后台线程中限制同时运行带有信号量的异步协程

redis - 如何获取tornadoredis的监听值

Java - 将分钟和秒更改为时间格式? (毫米:ss)

android - 从改造响应中获取 JSON 数组