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

标签 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"
                )
            } 
        }

在运输服务中:
suspend fun getBonuse() =
            withContext(Dispatchers.IO) {
                // some code here
            } // Dispatchers.Main

在 Activity 中:
import kotlinx.coroutines.*
private lateinit var poll: Job

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

    override fun onResume() {
        super.onResume()
        poll = mainViewModel.initPoll()
    }

它工作正常。但是当我转到另一个屏幕( Activity )时,它会抛出
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

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

最佳答案

您当前的协程实现受限于 ViewModel 的生命周期,并且 viewModelScope cancels after ViewModel has been cleared up (打开另一个 Activity 后会发生什么)。

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

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

关于android - 转到另一个屏幕时抛出 kotlinx.coroutines.JobCancellationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60185192/

相关文章:

kotlin - 异常后如何恢复流程

java - Android Views 的 WeakReference 安全吗?

go - 哪个协程(goroutines 和 kotlin coroutines)更快?

android - 使用ViewModel处理屏幕旋转而不会丢失数据-Android

java - 尝试从长字符串中获取字符串时出现 StringIndexOutOfBoundsException

android - 使用 Android Q ffmpeg 拒绝权限” : error=13, 权限被拒绝

android - ios opengl es 在干净的 C 上渲染纹理

android - 如何在ionic2中重新加载api url

Android App : can I use iOS sqlite? 如果没有,我如何从云 (Parse.com) 中的数据库创建本地数据库?

android - 如何使用 Kotlin Coroutines 正确地拥有待处理操作队列?