使用 Kotlin 协程了解作业的所有状态
Knowing all status of a Job with Kotlin coroutines
是否可以知道协程Job的所有状态?
我找到了这个功能扩展,但我无法访问 Job
的所有状态:
fun Job.status(): String = when {
isCancelled -> "cancelled"
isActive -> "Active"
isCompleted -> "Complete"
else -> "Nothing"
}
Job
类没有 isNew
、isCancelling
或 isWaitingForChildren
函数。为什么?
感谢 Drawn Roccoon,我找到了解决方案:
fun Job.status(): String = when {
isActive -> "Active/Completing"
isCompleted && isCancelled -> "Cancelled"
isCancelled -> "Cancelling"
isCompleted -> "Completed"
else -> "New"
}
此 link 中的更多信息:https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/
是否可以知道协程Job的所有状态?
我找到了这个功能扩展,但我无法访问 Job
的所有状态:
fun Job.status(): String = when {
isCancelled -> "cancelled"
isActive -> "Active"
isCompleted -> "Complete"
else -> "Nothing"
}
Job
类没有 isNew
、isCancelling
或 isWaitingForChildren
函数。为什么?
感谢 Drawn Roccoon,我找到了解决方案:
fun Job.status(): String = when {
isActive -> "Active/Completing"
isCompleted && isCancelled -> "Cancelled"
isCancelled -> "Cancelling"
isCompleted -> "Completed"
else -> "New"
}
此 link 中的更多信息:https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/