Search code examples
ioskotlinkotlin-multiplatformktorkmm

InvalidMutabilityException: mutation attempt of frozen kotlin.native.internal Exception


I'm very new to the Kotlin Multiplatform and Swift language, I have a problem with KMM only the iOS part, I have successfully run this on Android but it fails on IOS due to concurrency issues.

Kotlin code snippet :

@Throws(Exception::class)
    suspend fun getResponse(data: String): String {
        var response: String
        
        client.responsePipeline.intercept(HttpResponsePipeline.Transform) { (_, body) ->
                    when (context.response.status) {
                        HttpStatusCode.OK -> response = body as String
                    }
                }
                response = client.post(BASE_URL) {
                    contentType(ContentType.Application.Json)
                    body = data
        }
        return response
    }

iOS code snippet :

@State var response: String = ""

    Button("Click") {
        Repository().getResponse(data: "hello world") { data, error in
            if data != nil {
                response.self = "\(data)"
            }
        }
    }

I get HttpClient: {"output":"...","statusCode":200 } from the Api which I want but it fails anyways.

I tried wrap the post request with CoroutineScope(Dispatchers.Main){ withContext(Dispatchers.Default){}}

But no luck, any idea why?


Solution

  • Migrating to Ktor 2.0.0 solved many problems for me.