Fix kotlin.native.IncorrectDereferenceException issue on KMM project
The problem
The problem
Today, we encounter a problem, that is our project use KMM(Kotlin Multiplatform Mobile) for sharing code between Android and iOS, but after iOS side get the object from KMM, and used it on another thread, it will report error, and the app will crash. The error is like below:
Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared <kmmobject> from other thread
Solution for the problem
Because we return one object for the method, se the solution for the problem is calling .freeze()
method on iosMain
before return the object.