So I gave a talk about Kotlin/Native and I was und...
# coroutines
e
So I gave a talk about Kotlin/Native and I was under the impression that there was no
runBlocking
on K/N because I could never successfully get it to import. But @russhwolf pointed out that it is definitely there. I looked in my local
kotlinx-coroutines-core-1.1.1
and lo and behold, there it is. What am I missing about getting it to import in a
common
framework?
Relevant bit of `build.gradle`:
Copy code
commonMain.dependencies {
            api "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version"
            implementation "io.ktor:ktor-client:$ktor_version"
            implementation "io.ktor:ktor-client-core:$ktor_version"
            implementation "io.ktor:ktor-client-json:$ktor_version"
            implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"
        }

        commonTest.dependencies {
            implementation "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
            implementation "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
        }
r
It's not in common in the stdlib kotlinx, just jvm and native. So you need to add your own `expect`/`actual` declarations to link them.
d
It's not present in js target, hence it's not present in multiplatform. Use your own multiplatform declaration.
e
gooooot it.
will mess with it and see what i can figure out
r
You do something like this in common
expect fun <T> runBlocking(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T
And then something like this in JVM/Native
actual fun <T> runBlocking(context: CoroutineContext, block: suspend CoroutineScope.() -> T): T = kotlinx.coroutines.runBlocking(context, block)
See also the workaround posted in https://youtrack.jetbrains.com/issue/KT-22228 which further includes a js implementation wrapping around a Promise
(essentially the same in AndroidTest). Thanks @russhwolf for correcting my misunderstanding!
👍 1