Kotlin 协程未 运行 在 main 上启动

Kotlin coroutines not running launch on main

我有以下代码:

 Timber.d("Calling coroutine from thread: ${Thread.currentThread().name}")
    scope.launch {
        Timber.d("Current thread: ${Thread.currentThread().name}")
        runTest()
    }

 suspend fun runTest() {
    coroutineScope {
        launch(Dispatchers.Main) {
            Timber.d("Running from thread: ${Thread.currentThread().name}")
        }
    }
}

如果我运行呢。应用程序崩溃,日志中没有错误。 在我的日志中我看到:

Calling coroutine from thread: main

Current thread: DefaultDispatcher-worker-2

但我没有看到来自线程 运行 的条目:

这是在视图模型中完成的 我的示波器如下所示:

val scope: ViewModelCoroutineScope = ViewModelCoroutineScope(Dispatchers.Default)

class ViewModelCoroutineScope(
    context: CoroutineContext
) : CoroutineScope {

    private var onViewDetachJob = Job()
    override val coroutineContext: CoroutineContext = context + onViewDetachJob

    fun onCleared() {
        onViewDetachJob.cancel()
    }
}

我做错了什么?

移动到另一台机器后,我终于得到了一些关于 Dispatchers.MAIN 不工作的错误。

最后我所要做的就是将所有原始协程依赖项替换为:

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1")