https://kotlinlang.org logo
Title
s

serandel

07/05/2022, 7:35 AM
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
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'
composeVersion=1.1.1
coroutinesVersion=1.6.1
javaVersion=17
kotlinVersion=1.6.10
e

ephemient

07/05/2022, 7:40 AM
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

serandel

07/05/2022, 7:41 AM
that’s fab, thanks!
a follow-up question: my use case is something like this:
@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

ephemient

07/05/2022, 7:47 AM
you do not need to use the main dispatcher, it will use the composable's coroutinescope
s

serandel

07/05/2022, 7:48 AM
which is an appropiate one anyway
thanks!
e

ephemient

07/05/2022, 7:49 AM
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

serandel

07/05/2022, 7:51 AM
it makes sense
a

Adam Brown

07/05/2022, 6:36 PM
I do an expect/actual of a global val called "mainDispatcher" which each platform provides their main disaptcher to
e

ephemient

07/05/2022, 6:49 PM
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

orangy

07/05/2022, 8:17 PM
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?