Hi, quick question! Is there no `Dispatcher.Main` ...
# compose-desktop
s
Hi, quick question! Is there no
Dispatcher.Main
in Compose for Desktop? Or should I just depend on
kotlinx-coroutines-android
?
I’m getting this error when starting
Copy code
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Module with the Main dispatcher is missing. Add dependency providing the Main dispatcher, e.g. 'kotlinx-coroutines-android' and ensure it has the same version as 'kotlinx-coroutines-core'
Copy code
composeVersion=1.1.1
coroutinesVersion=1.6.1
javaVersion=17
kotlinVersion=1.6.10
e
you need to provide a main dispatcher as appropriate for your app's runtime. in the case of compose-desktop, https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-swing/ will work
✔️ 1
s
that’s fab, thanks!
a follow-up question: my use case is something like this:
Copy code
@Composable
fun App(stateFlow: StateFlow<MyState>) {
    val state: MyState by stateFlow.collectAsState(context = Dispatchers.Main)

    RenderUI(state)
}
where
RenderUI
is also a
@Composable
am i just being overly cautious by trying to make sure that the state flow is collected in the Main thread, which I understand as the UI one?
or by being collected inside a composable it’s going to be collected in that thread anyway?
e
you do not need to use the main dispatcher, it will use the composable's coroutinescope
s
which is an appropiate one anyway
thanks!
e
in theory composables do not necessarily use the main thread. in practice they currently do, but possibly in the future they'll do multi-threaded composition or something
👌🏻 1
👌 1
s
it makes sense
a
I do an expect/actual of a global val called "mainDispatcher" which each platform provides their main disaptcher to
e
you need a real main dispatcher for
delay
to work in kotlinx.coroutines 1.6.0, unless you set
-Dkotlinx.coroutines.main.delay=false
(but it looks like they rolled that back in 1.6.1)
o
There is
MainUIDispatcher
in
org.jetbrains.skiko
package
👀 4
e
it is still fine for an application to use
kotlinx-coroutines-swing
, it's being avoided in the libraries to prevent conflicts?
1870 Views