安卓工作室 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/