I am getting this error, even though I included th...
# compose-desktop
d
I am getting this error, even though I included the "kotlinx-coroutines-core" in the commonMain and the "kotlinx-coroutines-swing" in the jvmMain block of the shared gradle file:
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'
	at kotlinx.coroutines.internal.MainDispatchersKt.throwMissingMainDispatcherException(MainDispatchers.kt:77)
	at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.missing(MainDispatchers.kt:108)
	at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded(MainDispatchers.kt:92)
	at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:315)
	at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:26)
	at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:21)
	at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:88)
	at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:123)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:52)
	at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:43)
	at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
any idea how to solve this?
g
On desktop you need to provide a dispatcher based on what you want to use (swing/jfx) for example -
kotlinx-coroutines-jvm = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "coroutines" }
d
I have added "kotlinx-coroutines-swing" already, but I get this error
g
where do you have that added?
d
I added in the gradle file of the shared module
g
is your project desktop only or multiplatform? If so, you need to add it for the jvm configuration.
d
it's a multiplatform project, and I am using coroutines only in the shared module
I found the problem! I had created an extra dependencies block in the JVM!!!
my fault!
a
Is this working for you with compose-multiplatform > 1.5.11? When i tried to update last time i had similar issues but did not had the time to dig in too much
d
I am using Compose Multiplatform 1.6.1. It works good. It was just my mistake.
a
Hmm really strange, I'm seeing this only when updating to 1.6.0 and I already have swing coroutines in my desktopMain block. Would you mind posting some more of your gradle file?
Looks like Dispatchers.Main is not ideal to be used. After changing to Dispatchers.Default everything was fine again 🙂 I created this open class to use coroutines in ViewModels very similar to android, but maybe thats not the best idea 🤷
Copy code
open class ViewModel {
    protected val viewModelScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
}
p
@Daniele B I'm also using Compose Multiplatform 1.6.1. Where did you insert the coroutines swing dependency?
Copy code
desktopMain.dependencies { ... }
Here?
d
@Pedro Romano Barbosa yes, exactly