trashcoder
08/22/2022, 6:37 PMmain thread
for jvm
and dispaching
from it.
(detailed description in slack thread)trashcoder
08/22/2022, 6:38 PMandroid
and jvm
(windows).
i used to have some "not on main thread" warnings on android
which i could fix with observing (and dispatching
) on mainscheduler. but i never got any warnings for jvm
.
now i noticed this message in the jvm
log for the first time:
[MVIKotlin]: Main thread id is undefined, main thread assert is disabled
when i set the main thread in the jvm
main function using setMainThreadId(Thread.currentThread().id)
it breaks my implementation (which works fine on android
).
i cannot dispatch
from within scope.launch{}
anymore. but disatching
before launching works.
works without setting the main thread on jvm
and `android`:
private inner class ExecutorImpl : CoroutineExecutor<Intent, Unit, State, Msg, Nothing>() {
override fun executeAction(action: Unit, getState: () -> State) {
scope.launch {
dispatch(Msg.Booting)
works on jvm
when i set the main thread:
private inner class ExecutorImpl : CoroutineExecutor<Intent, Unit, State, Msg, Nothing>() {
override fun executeAction(action: Unit, getState: () -> State) {
dispatch(Msg.Booting)
scope.launch {
trashcoder
08/22/2022, 6:40 PMArkadii Ivanov
08/22/2022, 6:40 PMtrashcoder
08/22/2022, 6:50 PMArkadii Ivanov
08/22/2022, 6:53 PMsetMainThreadId
method?trashcoder
08/22/2022, 7:03 PMfun main() {
setMainThreadId(Thread.currentThread().id)
val lifecycle = LifecycleRegistry()
val root = root(DefaultComponentContext(lifecycle = lifecycle))
application {
val windowState = rememberWindowState()
val icon = painterResource("ic_launcher-desktop.png")
LifecycleController(lifecycle, windowState)
Tray(
icon = icon,
menu = {
Item("Beenden", onClick = ::exitApplication)
}
)
Window(
onCloseRequest = ::exitApplication,
state = windowState,
title = "Trashboard",
icon = icon
) {
App(root)
}
}
}
Arkadii Ivanov
08/22/2022, 7:07 PMmain
function runs not on the UI thread. You can try the following code:
SwingUtilities.invokeAndWait {
setMainThreadId(Thread.currentThread().id)
}
trashcoder
08/22/2022, 7:30 PMArkadii Ivanov
08/22/2022, 7:33 PMtrashcoder
08/22/2022, 7:45 PMfun main() {
val lifecycle = LifecycleRegistry()
lateinit var root: Root
SwingUtilities.invokeAndWait {
setMainThreadId(Thread.currentThread().id)
root = root(DefaultComponentContext(lifecycle = lifecycle))
}
application {
val windowState = rememberWindowState()
val icon = painterResource("ic_launcher-desktop.png")
LifecycleController(lifecycle, windowState)
Tray(
icon = icon,
menu = {
Item("Beenden", onClick = ::exitApplication)
}
)
Window(
onCloseRequest = ::exitApplication,
state = windowState,
title = "Trashboard",
icon = icon
) {
App(root)
}
}
}
thanks a lot! 🙂trashcoder
08/23/2022, 7:46 PMobserveOn(mainScheduler)
and then call my callback/event in subcribe which is implemented in the store
, so the store can dispatch
the new state directly. this does not work anymore because the (swing awt) main thread is not the same as the mainScheduler
. i solved this by removing observeOn(mainScheduler)
and wrapping the dispatch
in the store
with scope.launch{}
.
before:
observable<String> { emitter ->
...
}.observeOn(mainScheduler).subscribe {
onServerSentEvent(it)
}
onServerSentEvent = sse -> {
dispatch(Msg.SSE(sse))
}
after:
observable<String> { emitter ->
...
}.subscribe {
onServerSentEvent(it)
}
onServerSentEvent = sse -> {
scope.launch {
dispatch(Msg.SSE(sse))
}
}
2. I use an mqtt
client to subscribe
to a server (my roomba). the client has callbacks for listening to subscription messages. however, when i set the main thread, i can still connect successfully and subscribe but i never receive a subscription message in the subscription listener. so something seems to be blocking this...?
i tried suspending at several locations with several contexts/dispatchers but it never worked.
if i remove setMainThreadId
, i receive the messages again.
any idea/hint what i could do about it? (other than not setting the main thread of course 😉)trashcoder
08/23/2022, 7:54 PMArkadii Ivanov
08/23/2022, 7:59 PMArkadii Ivanov
08/23/2022, 8:03 PMArkadii Ivanov
08/23/2022, 8:08 PMtrashcoder
08/23/2022, 8:17 PMtrashcoder
08/23/2022, 10:03 PMArkadii Ivanov
08/23/2022, 10:46 PMtrashcoder
08/24/2022, 6:29 PMMainScope().launch {
onConnectionResult(true)
}
MainScope().launch {
onMessage(robotId, robotUpdateMessage)
}