gpt4 book ai didi

kotlin - Kotlin协程不在主服务器上运行启动

转载 作者:行者123 更新时间:2023-12-02 13:34:41 25 4
gpt4 key购买 nike

我有以下代码:

 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


但是我看不到 从线程运行的条目:
这是在viewmodel中完成的
我的范围如下所示:
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")

关于kotlin - Kotlin协程不在主服务器上运行启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59528120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com