简体   繁体   中英

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?

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.

迁移到Ktor 2.0.0为我解决了很多问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM