kotlin - 使用 Kotlin 协程了解作业的所有状态

标签 kotlin coroutine kotlin-coroutines kotlin-android-extensions

是否有可能知道协程 Job 的所有状态?

我找到了这个功能扩展,但我无法访问 Job 的所有状态:

fun Job.status(): String = when {
    isCancelled -> "cancelled"
    isActive -> "Active"
    isCompleted -> "Complete"
    else -> "Nothing"
}

没有isNew , isCancellingisWaitingForChildren Job 的功能类。为什么 ?

enter image description here

最佳答案

感谢 Draw Roccoon 我找到了解决方案:

fun Job.status(): String = when {
    isActive -> "Active/Completing"
    isCompleted && isCancelled -> "Cancelled"
    isCancelled -> "Cancelling"
    isCompleted -> "Completed"
    else -> "New"
}

enter image description here

此链接中的更多信息:https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/

关于kotlin - 使用 Kotlin 协程了解作业的所有状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60170920/

相关文章:

kotlin - 如何使用协程从 Room 返回单个(非 LiveData)对象

python - 基于生成器的协程看似无限递归

c# - 协程停止工作

kotlin - `SendChannel.offer` 、 `CompletableDeferred.complete` 等可以在协程之外调用吗?

android - 从 Android 中的挂起函数并行调用 Kotlin 协程

android - 撰写 UI 测试 - 如何断言文本颜色?

optimization - Sonar 报告中的认知复杂性是什么?

android - 类型不匹配 : inferred type is AutoMigration but KClass<*> was expected

android - 协程中的自定义拦截器

Kotlin 协程压缩三个流程