【发布时间】:2021-06-29 05:19:39
【问题描述】:
我正在尝试在 Kotlin Multiplatform 项目中使用 coroutines。我都没有这方面的经验。
我正在尝试调用这个函数
fun startFlow {
coroutineScope.launch {
withContext(defaultDispatcher) {
myFlow.collect { next -> onNext(next) }
}
}
}
coroutineScope on iOS 是这个
val defaultScope: CoroutineScope = object : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = SupervisorJob() + Dispatchers.Default
}
这不是给我这个问题的唯一调用,事实上所有对coroutines 的调用似乎都因这个错误而失败:
kotlin.IllegalStateException: There is no event loop. Use runBlocking { ... } to start one.
这就是我导入库的方式
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3")
}
}
我正在使用Kotlin 1.4.31。这个问题只存在于iOS,Android 完美运行。
我不明白我是否遗漏了什么。
【问题讨论】:
标签: kotlin-coroutines kotlin-multiplatform kotlin-multiplatform-mobile