Is coroutines supported in Kotlin/Native or Common...
# kotlin-native
k
Is coroutines supported in Kotlin/Native or CommonMain modules?
s
Yes but limited to both questions.
In native, coroutines can only run on the main thread. In Common there is no
runBlocking
because not all targets are multi-threaded (Javascript).
k
But I am unable to use them in iOS or common target. Import works only in Android Activity
j
Yes but they are limited. Can you show example code and build.gradle?
k
build.gradle of SharedLib module
iosMain/actual.kt file
build.gradle of app module, that has coroutines imported and works fine in Android MainActivity.kt
@Jeremy I have posted the screenshots. Please let me know what I am doing wrong here!
j
Are you getting compiler errors or only in IDE? IDE support isn't complete (yet)
k
I am getting compilation errors
j
You need to include the coroutines in the common dependencies
in common
implementation "org.jetbrains.kotlinxkotlinx coroutines core common$coroutines_version" implementation "org.jetbrains.kotlinxkotlinx coroutines test$coroutines_version"
k
Okay, let me try that
j
and in ios use: implementation "org.jetbrains.kotlinxkotlinx coroutines core native$coroutines_version"
k
Let me take a look at that
j
k
It works in the common module
iOS still doesn’t work
I referred to those links you shared. Those were helpful. But for me serialization is giving trouble as well. “Plugin with id ‘kotlinx-serialization’ not found”
j
You setup the ios specific coroutine dependency to your gradle?
k
yes, I did
the core-native one
the file looks like this now
j
KN coroutine doesn't have GlobalScope, you need to pass coroutine context in from native iOS
d
👍 1
k
Thank you Chris, I will take a look!