https://kotlinlang.org logo
Title
y

yusuf3000

06/19/2018, 11:05 AM
I’ve got a particular use case for K/N where I want all my kotlin code to run on a serial background queue. This queue is created on the iOS side using
DispatchQueue.global(qos: .userInteractive).sync { }
. I also keep a reference to this queue. Then from the iOS side i want to call methods on the same K/N objects and again, I use the same serial queue and call those methods. But you cannot share objects between threads in K/N and a serial queue can still use a different thread each time, it’s up the OS. (I’m aware that you can share objects in K/N using
Workers
but all this code is in the common module rather than the iOS module so I do not have access to it) So would i need to change the implementation so i use the same thread each time from iOS or is using a serial queue that could potentially use multiple threads suffice?
I have tested this out using just a custom serial queue, and the app functions fine, but I’ve also been quite lucky that it keeps using the same thread everytime i use the queue.
s

svyatoslav.scherbina

06/19/2018, 11:30 AM
Currently this case is not supported directly, but we are considering it and going to design and implement support for serial queues being exclusive object owners just as workers. Could you please provide more details? How do you achieve that:
Then from the iOS side i want to call methods on the same K/N objects
?
y

yusuf3000

06/19/2018, 11:31 AM
Ah that would be great!
Sure, I have a class in Kotlin that functions as a singleton and needs to persist throughout apps lifecycle. I create it on app launch and send methods to it throughout the lifecycle too. The queue this object is created on needs to be serial and a non main thread as it needs to block.
So for the time being I have to use a different solution than using a serial queue correct?
s

svyatoslav.scherbina

06/19/2018, 11:46 AM
And where do you store reference to your object?
y

yusuf3000

06/19/2018, 11:46 AM
On the iOS side
It’s a property of the AppDelegate
s

svyatoslav.scherbina

06/19/2018, 11:53 AM
It’s a property of the AppDelegate
Well, it would be OK if you write and read this field only from that serial queue. Does your Kotlin object have any mutable fields or refer (directly or indirectly) to some Kotlin object with mutable fields?
y

yusuf3000

06/19/2018, 11:54 AM
Let me confirm and get back to you
a

ahmedre

06/19/2018, 12:00 PM
does the immutability (strictly within the Kotlin layer) only matter because we can’t be sure that the serial queue on iOS will always use the same thread?
s

svyatoslav.scherbina

06/19/2018, 12:26 PM
If object is immutable, then it is possible to workaround the issue by freezing it thus making shareable among all threads. So yes, it is related.
👍 1
y

yusuf3000

06/19/2018, 12:31 PM
It it possible to use the konan plugin alongside the kotlin-platform-common plugin so we get access to the freezing object methods?
s

svyatoslav.scherbina

06/19/2018, 12:45 PM
I’m not sure, but why does it matter? If you compile your code to framework, then you can just add Kotlin/Native-specific code to the compilation of this framework in Gradle.