I have anther problem. Is there any way to impleme...
# kotlin-native
j
I have anther problem. Is there any way to implement a mechanism on native like Handler.post(runnable) on android? when I accomplish the backend job in Worker, I want switch to calling thread (Main thread), Could anyone help me?
m
You'll need some kind of event loop
A while loop with some kind of message queue. Maybe https://github.com/touchlab/Stately#stately-iso-collections could help. Not sure if there is something alreeady there
j
I haven't find the message queue in the stately library, which class do you mean?
m
Really not sure there is something available "on the shelf" indeed. You might have to implement it yourself using more primitive classes
👍 1
a
With Reaktive you can do this as follows:
mainScheduler.submit {}
👍 1
k
Stately has nothing for this. The main thread has different mechanisms on each platform, and outside of Android and iOS, I honestly don’t know what they would be. Reaktive sounds like a good bet.
👍 1
j
@Arkadii Ivanov How did Reaktive implements switching thread from the subscribeOn scheduler to the observeOn scheduler after accomplished the background job? I searched from the source code for a long time but I couldn't find it,Could you give me some guidance?
a
@Jeff Beyond
subscribeOn
and
observeOn
are two independent operators, so there is no such a thing like "switching thread from the subscribeOn scheduler to the observeOn". You can check how these operators are implemented for `Observable`: subscribeOn and observeOn. Other types (
Single
,
Maybe
and
Completable
) have similar implementations. The
Scheduler
interface is the abstraction over threading. There are multiple schedulers provided by the library. They are implemented separately for every target. What targets do you support?
👍 1
j
@Arkadii Ivanov We support the targets including Android, iOS, Linux, macOS, and maybe Windows in future
a
Alright. For plain Linux there is no any main thread looper by default, as well as for plain JVM. By default on this platforms Reaktive uses a separate thread as "main" thread. You will need to setup main scheduler by yourself, here is an example of how you can integrate Reaktive with GTK3's main scheduler: https://github.com/badoo/Reaktive/blob/master/sample-linuxx64-app/src/linuxMain/kotlin/com/badoo/reaktive/sample/linux/MainScheduler.kt
Also Reaktive does not support Windows at the moment. But we are considering this.
For Android, iOS and macOS it will work out of the box.
👍 1
j
@Arkadii Ivanov It really helps me, thanks a lot.
🎉 1