Home > Enterprise >  InvalidMutabilityException: mutation attempt of frozen kotlin.native.internal Exception
InvalidMutabilityException: mutation attempt of frozen kotlin.native.internal Exception

Time:05-23

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?

CodePudding user response:

Assuming you're on recent Kotlin and library versions, you should enable the new memory model. Put this in gradle.properties

kotlin.native.binary.memoryModel=experimental

See KaMP Kit for an example.

CodePudding user response:

Migrating to Ktor 2.0.0 solved many problems for me.

  • Related