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

Damiano Giusti

12/13/2021, 2:34 PM
Hi, is there a way to use Ktor 1.6.7 with Coroutines 1.6.0-RC2 on Kotlin/Native to take advantage of the new memory model? I get an error when I try to use the HttpClient on iOS, which is thrown by this function which is called in the init block of HttpClient. Looks like I’m not able to use a Coroutines version different than a
native-mt
one
h

hfhbd

12/13/2021, 2:44 PM
Currently, you can only use Ktor with a
native-mt
version, which will be published after
1.6.0
official release. There is wip to support non native-mt version: https://github.com/ktorio/ktor/tree/e5l/coroutines
d

Damiano Giusti

12/14/2021, 8:11 AM
I see, thanks! I tried with coroutines
1.5.2-native-mt
, and now the only limitation on iOS is that suspend functions must be called from the main thread
r

rudolf.hladik

12/14/2021, 10:32 AM
I use
Copy code
kotlin = "1.6.0"
coroutines = "1.6.0-RC"
ktorVer = "1.6.2-native-mm-eap-196"
with
Copy code
# enable new native memory model
kotlin.native.binary.memoryModel=experimental
kotlin.native.binary.freezing=disabled
and it works all fine
👀 1
j

John O'Reilly

12/14/2021, 10:33 AM
One small gotcha with that version of ktor is that it doesn't support M1.... that support was added I think in 1.6.5
d

Damiano Giusti

12/14/2021, 11:13 AM
thanks guys. @rudolf.hladik do you get the
Calling Kotlin suspend functions from Swift/Objective-C is currently supported only on main thread
error with this setup? I just tried with your configuration but coroutines 1.6.0-RC2 and I’m still getting the error. Btw Kotlin 1.6.10 has just been released 😄
h

hfhbd

12/14/2021, 12:44 PM
This is still required.
r

rudolf.hladik

12/15/2021, 11:00 AM
I don’t get this error beacause I use Arkitekt usecases library, for K1.6 and new native MM use version
0.2.0-SNAPSHOT-MM
. But you can basically use something like this
Copy code
fun <T> execute(scope: CoroutineScope, suspendBlock: suspend ()-> T, onSuccess: (T) -> Unit, onError: (Throwable) -> Unit): Job {
    // launch on main dispatcher
    return scope.launch {
        runCatching {
            suspendBlock() // suspend block can be called on different dispatcher
        }.fold(onSuccess,onError) // onSuccess, onError called on main
    }
}
5 Views