https://kotlinlang.org logo
#ktor
Title
# ktor
t

tylerwilson

07/13/2021, 7:53 PM
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

Big Chungus

07/13/2021, 8:46 PM
Define your own with expect/actual
j

Javier

07/13/2021, 9:05 PM
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

tylerwilson

07/13/2021, 9:21 PM
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

louiscad

07/14/2021, 7:00 AM
You can use Dispatchers.Main with the native-mt version of kotlinx.coroutines
MainScope() would work as well.
t

tylerwilson

07/14/2021, 1:10 PM
I changed to using ktor-client-ios for macOS, and also turned on this switch. All working now.
3 Views