CoroutineScope生命周期问题

时间:2020-08-20 17:38:19

标签: android kotlin kotlin-coroutines

当前,我泄漏了GlobalScope.launch()创建的协程,其中的Job在某些情况下并未被取消。我想知道是否在我的函数中创建了CoroutineScope(),以及是否在收集垃圾后创建CoroutineScope()的范围取消了与我的自定义范围相关联的工作?

例如

class MyActivity: Activity {

    fun myCustomCoroutineScopeScope() {
       job = CoroutineScope.launch {
           endlessFunctionCall()
       }
    }

    suspend fun endlessFunctionCall() {
        while (isActive) {
         // perform work
        }
    }

    // sometimes not invoked when application terminates
    fun cleanup() {
        job.cancel()
    }
}

0 个答案:

没有答案