I have a working KMM module using Ktor client on i...
# ktor
t
I have a working KMM module using Ktor client on iOS and Android. Trying to add macOS support. I use the following pattern:
Copy code
GlobalScope.launch {
    launch(ApplicationDispatcher) {
       client.get {
       }
    }
}
but when testing with a macOS SwiftUI app, I get the following:
Copy code
Error: There is no event loop. Use runBlocking { ... } to start one.
but runBlocking cannot be used in the common code. Any solutions to this? Is the above ‘canonical’ way to make Ktor client calls in common code?
b
Define your own with expect/actual
j
runBlocking exists in all targets except JS. I have a lib that you can use it in common code if you are not using JS. For testing I have another artifact based on what I could see in the turbine repository by cashapp/square https://github.com/JavierSegoviaCordoba/run-blocking-kmp
t
I incorporated an expect/actual of a runBlocking wrapper, ran it and the same result. I suspect it is related to some other setup needed for Ktor on macOS. Will keep trying things. Thank you both for the links.
l
You can use Dispatchers.Main with the native-mt version of kotlinx.coroutines
MainScope() would work as well.
t
I changed to using ktor-client-ios for macOS, and also turned on this switch. All working now.