I have an issue with my Kotlin Multiplatform project, I created my data models in a shared project (in my github example i called this Concurrency), then when i run this in my iOS project and access the variable from multiple threads, i'm getting the following exception:
Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared sample.Concurrency@1f64468 from other thread
I need to be able to access variables concurrently from different threads in Swift, especially as i use AsyncDisplaykit in my project, which is always jumping from threads.
Here is a link to the issue i am facing:
https://github.com/Reedyuk/Concurrency-Issue-Kotlin-Native
Open the iOS-Example folder and run to get the issue, or open the whole folder in intelij and run buildForXcode and copy the framework file across into the ios project.
I have same issue in my project. Only working way for now was to freeze my objects, and then make a copy on another thread, but there has to be another easy way to do it.
You can check this issue, maybe you will figure out how to apply olonho's solution into Swift world.
Im at an end pass, especially given that my ui level needs access to the objects to determine the size of a view, this is accessed on the background thread(Texturegroup/asyncdisplaykit) so that its performant. Its a real deal breaker, as i really want to use Kotlin Multiplatform, and I'm sure there is more cases where this will happen, not just in the ui layer.
You have to "freeze" your instances with kotlin.native.concurrent.
But, I guess, neither "@SharedImmutable" nor ".freeze()" are avail in mpp projects ¯_(ツ)_/¯
Well the problem is, sometimes you don't want to freeze object, but pass it to another thread (and change thread that owns this object). I tried to do it with DetachedGraphObject, but it didn't work (I don't want to use Workers, I would like to call DispatcherQueue from Swift, then detach graph and attach it on main thread)
did you accomplish this @Coneys ? I am trying to do exactly the same thing
No, I gave up. For now Kotlin Native and threading system is too complicated. It is faster for us to duplicate code on iOS and Android ;/
We are having the exact same problem. Can you guys add the @SharedImmutable" or ".freeze()" features to MPP?
Btw. All our data is immutable so if it is easier to grant us read-only access on Kotlin objects in threads other then the Main thread that would be awesome.
About our plans: https://blog.jetbrains.com/kotlin/2020/07/kotlin-native-memory-management-roadmap/
YouTrack issue: https://youtrack.jetbrains.com/issue/KT-42296
Can you guys add the @SharedImmutable" or ".freeze()" features to MPP?
Not sure if it is possible to make this entirely correctly, because there are no alternatives to these features on other platforms, and using dummy implementation might be confusing.
Trivial workaround is to declare these in your project.
Most helpful comment
Im at an end pass, especially given that my ui level needs access to the objects to determine the size of a view, this is accessed on the background thread(Texturegroup/asyncdisplaykit) so that its performant. Its a real deal breaker, as i really want to use Kotlin Multiplatform, and I'm sure there is more cases where this will happen, not just in the ui layer.