转到另一个屏幕时抛出 kotlinx.coroutines.JobCancellationException

a_s*_*ber 5 android kotlin-coroutines

安卓工作室 3.6

在一个屏幕上,我通过这样的协程轮询:

 fun initPoll() =       
        viewModelScope.launch(Dispatchers.Main) {
            var errorMessage = ""
            try {
                while (true) {
                    val balanceValue: BigDecimal = TransportService.getBonuse()
                    delay(1000)
                }
            } catch (e: CancellationException) {
                Debug.e(
                    TAG,
                    "initPoll: error_message = ${e.message}, ex = $e"
                )
            } catch (e: Throwable) {
                Debug.e(
                    TAG,
                    "initPoll: error_message = ${e.message}, ex = $e"
                )
            } 
        }
Run Code Online (Sandbox Code Playgroud)

在运输服务中:

suspend fun getBonuse() =
            withContext(Dispatchers.IO) {
                // some code here
            } // Dispatchers.Main
Run Code Online (Sandbox Code Playgroud)

在活动中:

import kotlinx.coroutines.*
private lateinit var poll: Job

 override fun onPause() {
        super.onPause()
        poll.cancel()
    }

    override fun onResume() {
        super.onResume()
        poll = mainViewModel.initPoll()
    }
Run Code Online (Sandbox Code Playgroud)

它工作正常。但是当我转到另一个屏幕(活动)时,它会抛出

02-12 11:34:17.115 E/com.myproject.MainViewModel(17685): initPoll: error_message = StandaloneCoroutine was cancelled, ex = kotlinx.coroutines.JobCancellationException: StandaloneCoroutine was cancelled; job=StandaloneCoroutine{Cancelling}@aa5d4a1
Run Code Online (Sandbox Code Playgroud)

转到另一个屏幕后,我需要停止轮询。

Ale*_*fts 11

您当前的协程实现受限于 ViewModel 的生命周期,并viewModelScope 在 ViewModel 被清除后取消(打开另一个 Activity 后发生的情况)。

因此,如果您的目标是在导航到另一个 Activity 后停止轮询奖金,则无需使用 隐式停止协程poll.cancel(),只需让它viewModelScope完成工作即可。否则,如果您想在清除 ViewModel 后继续轮询,请考虑使用一些上部(可能是全局)范围。

JobCancellationException如果您想在代码中避免使用CoroutineExceptionHandler作为协程上下文的补充,而不是 try/catch,它旨在处理协程中未捕获的异常并忽略已取消的异常。