sushma nayak
05/05/2021, 8:22 PMkotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen
exception.
Dependencies info -
kotlin = "1.4.30"
coroutines = "1.4.2-native-mt"
serialization = "1.0.1"
ktor = "1.4.2"
This is what I’m doing -
class Manager {
var user: User
init(){
GlobalScope.async(context = Dispatchers.Default) {
val result = callToSuspendFunction
user = result //ERROR ON THIS LINE
}
}
How to achieve something like this?uli
05/05/2021, 8:27 PMuser
was probably created on the main thread and is written from default dispatcher.sushma nayak
05/05/2021, 8:47 PMVitor Prado
05/05/2021, 8:58 PMMainScope
or using the Dispatchers.Main
KamilH
05/06/2021, 5:44 AMAtomicReference
. In your case it would simply be:
val user: AtomicReference<User?> = AtomicReference(null)
If you’re writing a multiplatform code you could be interested in on of those two libraries:
https://github.com/Kotlin/kotlinx.atomicfu
https://github.com/touchlab/Stately
You can read more about why it’s needed here:
https://kotlinlang.org/docs/mobile/concurrent-mutability.html