【发布时间】:2020-06-16 14:09:41
【问题描述】:
我对协程还是很陌生(但已经非常喜欢它们了)。我有这个错误(和崩溃),不知道这意味着什么。什么是 UndispatchedCoroutine?怎么取消的?
kotlinx.coroutines.JobCancellationException: UndispatchedCoroutine 被取消; job="coroutine#8":UndispatchedCoroutine{Cancelled}@fe7d397
编辑: 为了提供一些上下文,我使用一个 Actor 在协程中执行消息处理:
@OptIn(kotlinx.coroutines.ObsoleteCoroutinesApi::class)
internal inner class HandlerFPMsg : Handler() {
private val msgActor = coroutineScope.actor<Pair<Int, Long>>(Dispatchers.Default, capacity = Channel.UNLIMITED) {
for(msg in channel)
handleMessageWorker(msg.first, msg.second)
}
override fun handleMessage(msg: Message) {
msgActor.offer(Pair(msg.what, msg.data.getLong("ID",-1L)))
super.handleMessage(msg)
}
private suspend fun handleMessageWorker(what: Int, id: Long) {
if (what != 0x92) Log.d("Messenger123", "Message Received: [" + what.toString(16) + "]")
when (what) {
MsgConstants.MSG_CONSTANT1 -> {
someFunction()
Log.e(TAG, "${e.message}")
}
MsgConstants.MSG_CONSTANT2 -> {
if (id != lastId) return
// -----------------------
if (aSuspendingFunction()) {
msgService?.let { msgr -> aClass.sendAmsg(msgr) }
} else {
// some comment
someFunction()
}
}
}
}
}
如果我用 try catch 块包装 handleMessageWorker(msg.first, msg.second) 似乎可以工作(但我不知道代码的正确性如何......)
【问题讨论】:
-
Undispatched 协程是一种可以在同一个调度程序上嵌套上下文的状态,这本身不是问题。你的代码到底是做什么的?
-
@AlexeySoshin 我更新了我的问题
标签: android kotlin kotlin-coroutines