I'm trying to use coroutines
in a Kotlin Multiplatform
project. I'm not experienced in either.
I'm trying to call this function
fun startFlow {
coroutineScope.launch {
withContext(defaultDispatcher) {
myFlow.collect { next -> onNext(next) }
}
}
}
coroutineScope
on iOS
is this
val defaultScope: CoroutineScope = object : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = SupervisorJob() + Dispatchers.Default
}
This is not the only call that gives me this problem, in fact all calls to coroutines
seem to fail with this error:
kotlin.IllegalStateException: There is no event loop. Use runBlocking { ... } to start one.
This is how I import the library
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3")
}
}
I'm using Kotlin
1.4.31. This problem is only present in iOS
, Android
works flawlessly.
I don't understand if I'm missing something.