If I am using a Kotlin library in a Swift applicat...
# kotlin-native
s
If I am using a Kotlin library in a Swift application, and I create one of the Kotlin objects in Swift code, does the object have the same threading restrictions as in Kotlin, or can I pass the object between threads safely (basically as if it was a Swift or obj c)?
👍 2
t
What threading restrictions are you referring to exactly?
Shot in the dimly lit room here but I think what you're concerned about is a JVM issue. While I haven't dug too deep into the produced bytecode, it seems to be similar to C so it should be as thread safe as C. That is, it isn't. Should be fine passing them around, internally it's just passing memory pointers more or less.
k
Kotlin objects in native are reference counted, so if the number of references to them goes to zero, they are released. "Frozen" objects have atomic reference counting, and normal objects have non-atomic reference counting. If you create a Kotlin object from Swift, I don't think the Kotlin object itself would have anything special about it, so passing it to another thread would likely be problematic, but I haven't tested that, so I can't say for sure. But my guess is, no, you can't. With regards to "as thread safe as C", the data integrity and "thread safety" isn't so much the big problem as is memory management. If you pass non-frozen data around threads in Kotlin native you can have memory leaks and/or over-deallocation.
o
Yes, Kevin is correct, if you want to have MT safe Kotlin object - freeze it, not matter if it is associated with Objective-C object or not