https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
j

Jake

09/08/2020, 7:10 PM
When starting a new KMM application, I ran into a crash on iOS that I’m trying to make some sense of. The error message is:
Uncaught Kotlin exception: kotlin.native.concurrent.FreezingException: freezing of com.ramseysolutions.proportalspike.shared.Globals.Companion@f13308 has failed, first blocker is HttpClient[io.ktor.client.engine.ios.IosClientEngine@f13488]
The offending code was:
Copy code
kotlin

val json = kotlinx.serialization.json.Json { ignoreUnknownKeys = true }
fun createHttpClient() = HttpClient() {
    install(JsonFeature) {
        serializer = KotlinxSerializer(json)
    }
}


class Globals {
    companion object {
        client = createHttpClient()
    }
}
The iOS application utilizes that client in its first view. I noticed in my debugging, that the breakpoints in my iOS project were never getting hit. So I determined the crash was happening before the class that was referencing
client
was ever created. Working that theory, I was able to stop the crash from occurring and achieve the correct results on iOS and Android by removing that
client
property from the
companion object
and by creating a global variable in the iOS project:
Copy code
// Kotlin Shared

class Globals {
    client = createHttpClient()
}
Copy code
// iOS
let globals = Globals()
After resolving that, I asserted that a simple global variable would fail also, and I was correct when I tested that. My questions are: what was causing the failure? Are Coroutines unavailable until a certain point in the iOS application lifecycle? And what resources or tools can I use to figure that type of thing out on my own?
i

Ivann Ruiz

09/08/2020, 7:33 PM
Seems like this is a known issue, have a similar issue, have been looking for days and finally found this https://github.com/ktorio/ktor/issues/1550 people are reporting that reverting from ktor
1.4.0
to
1.3.2-1.4.0-rc
works just fine
Ok just tested reverting back the versions and my
httpClient
is not getting frozen anymore. Conclusion ktor
1.4.0
for iOS is broken.
j

Jake

09/08/2020, 7:49 PM
httpClient
is not getting frozen anymore.
The failure I’m getting was that it failed to freeze the client when initialized as a static/global property, But I get success when it’s not. I’m newer to the Kotlin freezing thing, but it seems to be working to me… But I would like to know how you’re checking if the freezes are successful or not. What tools do you use to inspect that?
i

Ivann Ruiz

09/09/2020, 1:35 PM
I use the good old "_*run it and see if it breaks*_" method. So far I've only worried about freezing on the iOS side when something cracks. Buuut I did noticed that the guys at Touchlab created a library to avoid the freezing, not sure how successful that was. This is their sample project and the library they use for this is
co.touchlab:stately-common:1.1.0
d

dambakk

09/10/2020, 1:15 PM
I got the same exception and tried to downgrade ktor as suggested, but it did not help. I found out that I had an
object
with the httpClient as a variable and that the object was not marked as
@ThreadLocal
. Annotating the object fixed my issues. I also had problems upgrading to kotlin 1.4 with the ios project using the Firebase Performance pod. Remove this resolved my issues.
17 Views