CoroutineExecutor exception when using scope.launch #71
-
I was trying to make use of the My code structure inside the executor was planned as follow: private inner class ExecutorImpl : CoroutineExecutor<Intent, Action, State, Message, Nothing>() {
override fun executeIntent(intent: Intent, getState: () -> State) {
when (intent) {
is Intent.LoadPosts -> loadPosts()
// ...
}
}
private fun loadPosts() = scope.launch {
dispatch(Message.PostsLoading)
// Get a result from network transactions inside of withContext(Dispatchers.Unconfined)
val result = withContext(Dispatchers.Unconfined) { /* ... */ }
// Dispatch messages and labels in scope.launch again
result.onSuccess { dispatch(Message.PostsLoaded(it)) }
} The above code throws in one of my projects an error when calling I researched this topic a bit to figure out why it fails and realized that According to the main dispatcher documentation and to the release notes of Compose 1.1.1 it looked like I just had to add the following dependency: runtimeOnly(Deps.JetBrains.KotlinX.Coroutines.swing) This however didn't solve the problem. I even tried to override things like below: import com.arkivanov.mvikotlin.core.utils.setMainThreadId
import com.badoo.reaktive.coroutinesinterop.asScheduler
import com.badoo.reaktive.scheduler.overrideSchedulers
import kotlinx.coroutines.Dispatchers
fun main() = application {
overrideSchedulers(main = Dispatchers.Main::asScheduler) // I am not sure if that overrides things for reaktive or coroutine executors
setMainThreadId(Thread.currentThread().id) // this suppresses "[MVIKotlin]: Main thread id is undefined, main thread assert is disabled", but looks dirty
// ...
} but with no success either. The problem disappears when I use
It feels like I am missing something and I don't properly understand anymore the way things work. My question is now, what would be the proper setup / structure for executing network transactions and dispatching messages in a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It seems that the issue you are having is actually not related to MVIKotlin, but rather a general coroutines question. You should definitely use The error First of all, please double check that you are not using Re: |
Beta Was this translation helpful? Give feedback.
It seems that the issue you are having is actually not related to MVIKotlin, but rather a general coroutines question. You should definitely use
Dispatchers.Main
, but as you noticed JVM doesn't have a main dispatcher by default. You are correct, for Compose for Desktop you should addkotlinx-coroutines-swing
as a dependency. It will add the main dispatcher for Swing.The error
Module with the Main dispatcher had failed to initialize.
suggests that the main dispatcher is not missing, but failed to initialise. It's hard to say why without the full stack trace, but most likely some dependency (maybe transitive) brought the Main dispatcher for Android.First of all, please double check that y…