Is anyone using ktor + reaktive on native targets?...
# reaktive
s
Is anyone using ktor + reaktive on native targets? What does your setup look like and have you run into any issues?
a
Hey, you can check the notice about Ktor in the Readme: https://github.com/badoo/Reaktive/blob/master/README.md#coroutines-interop
In general I tried Ktor multiple times, it's very easy to fall into the freezing trap and currently Ktor crashes in this case. The stack traces sometimes meaningless and overall it's hard to debug.
o
This is a simplified version of what we do at Skip
Copy code
@Serializable
data class Post(val id: Int, val title: String, val body: String)

typealias GetPostResult = (Post?) -> Unit

interface TodoApi {
    fun getPost(id: Int, callback: GetPostResult)
}

class TodoApiImpl : TodoApi {

    private val client: HttpClient =
        HttpClient(httpEngine) {
            install(JsonFeature) {
                serializer = KotlinxSerializer(Json(JsonConfiguration.Stable.copy(ignoreUnknownKeys = true, isLenient = true)))
            }
        }

    init {
        client.ensureNeverFrozen()
    }

    override fun getPost(id: Int, callback: GetPostResult) {
        GlobalScope.launch(Main) {
            callback(
                try {
                    client.get<Post>("<https://jsonplaceholder.typicode.com/posts/$id>")
                } catch (e: Throwable) {
                    null
                }
            )
        }
    }
}
ensureNeverFrozen
is from
reaktive-utils
httpEngine
is an expected
HttpClientEngine
which resolves to OkHttp in Android and
NSURLSession
in iOS
a
Yeah this will work. Except the returned Job is not used, so it is not possible to cancel. If you need cancellation, then you should care to not freeze the Job as well.
o
That's right
s
@Arkadii Ivanov Got it. The interop section in the readme was added a few months ago so I was wondering if anything has changed. I remember coming across mentions of a preview branch of ktor where native support has improved. @Omar Mainegra This is useful, thank you!
👍 1
a
Multithreaded native coroutines are in a separate branch currently and are published separately. I'm not sure about Ktor, lastest information I remember is it still crashes. Anyway Reaktive uses stable version of coroutines, they not multithreaded.
My personal opinion - I would avoid Ktor at the moment. They have 100+ open bugs and 500+ open issues. Is use simple expect/actual for jvm and Android when needed.
o
We are using ktor with coroutines
1.3.5-native-mt
, no crashes so far (beyond
InvalidMutabilityException,
etc), good point about their GH status tho
Is important to enforce the right Ktor version for K/N, something like:
Copy code
configurations.all {
    resolutionStrategy {
        force "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:${versions.coroutines}"
        force "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:${versions.coroutines}-native-mt"
    }
}