I am following the sample project that JetBrains h...
# multiplatform
r
I am following the sample project that JetBrains have - the sample code is here https://github.com/KaterinaPetrova/kmm-ktor-sample It works after updating some dependencies. Look at this code here:
Copy code
class Greeting {
    private val httpClient = httpClient() {
        install(Logging) {
            level = LogLevel.HEADERS
            logger = object : Logger {
                override fun log(message: String) {
                    Napier.v(tag = "HTTP Client", message = message)
                }
            }
        }
        install(JsonFeature) {
            val json = Json { ignoreUnknownKeys = true }
            serializer = KotlinxSerializer(json)
        }
    }.also { initLogger() }

    @Throws(Throwable::class)
    suspend fun greeting(): String {
        return "${getHello().random().string}, ${Platform().platform}! X"
    }

    private suspend fun getHello(): List<Hello> {
        return httpClient.get("<https://gitcdn.link/cdn/KaterinaPetrova/greeting/7d47a42fc8d28820387ac7f4aaf36d69e434adc1/greetings.json>")
    }
}
I knew I wanted to do some json manipulation so I made the Json instance an instance variable:
Copy code
class Greeting {
    private val json = Json { ignoreUnknownKeys = true }
    private val httpClient = httpClient() {
        install(Logging) {
            level = LogLevel.HEADERS
            logger = object : Logger {
                override fun log(message: String) {
                    Napier.v(tag = "HTTP Client", message = message)
                }
            }
        }
        install(JsonFeature) {
            serializer = KotlinxSerializer(json)
        }
    }.also { initLogger() }

    @Throws(Throwable::class)
    suspend fun greeting(): String {
        return "${getHello().random().string}, ${Platform().platform}! X"
    }

    private suspend fun getHello(): List<Hello> {
        return httpClient.get("<https://gitcdn.link/cdn/KaterinaPetrova/greeting/7d47a42fc8d28820387ac7f4aaf36d69e434adc1/greetings.json>")
    }
}
and now iOS crashes with an error:
Copy code
Function doesn't have or inherit @Throws annotation and thus exception isn't propagated from Kotlin to Objective-C/Swift as NSError.
It is considered unexpected and unhandled instead. Program will be terminated.
Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen com.jetbrains.kmmktor2.Greeting@3dbf5e8
Anyone know why?
l
Yes I know why: mutability is not currently supported for things that are seen from more than one thread, aka. shared XOR mutable. There's some info about that in KMM docs I think. You can also search for "Stranger threads", a series of articles written by Kevin Galligan on that topic.
a