I am upgrading an app I wrote a while ago (when co...
# compose-desktop
y
I am upgrading an app I wrote a while ago (when compose desktop what a separate entity) to the latest version of compose (1.3.0). I have the following code:
Copy code
fun CoroutineScope.uiTimer(period: Duration,
                           block: suspend CoroutineScope.() -> Unit): Job {
  return launch(Dispatchers.Main) {
    while(isActive)
    {
      delay(period.inWholeMilliseconds)
      if(isActive)
        block(this)
    }
  }
}
which now fails with this error:
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'
Since it is a desktop app, I am not sure that the suggestion is the right one... the whole point was to create a timer that runs in the UI thread. Any idea what is the "right" way to do this with a more recent version of compose?
e
Compose Desktop used to install the Swing main dispatcher, now it doesn't
do you really want to think about about a "main thread" with compose, though?
even if composition is single threaded now, that doesn't mean it'll continue to be in the future (multithreaded composition is possible in the future)
y
Technically I need to start a separate thread that update my model. And clearly updating it in the UI thread/main thread ensured that there was no race conditions
So what is the "proper" way to do this in compose: meaning a separate timer/thread that needs to update a model that is rendered by compose?
e
any thread is fine
y
That will undoubtedly create race conditions....
I'm updating multiple fields of the model in the callback so if the UI is recomposing it will be in a non consistent state
e
if it's done through MutableState you always get a consistent view
if you are directly exposing mutable properties from your model perhaps you need to reconsider
a
You’re probably specifying the dependencies manually. You should instead do it like this:
Copy code
plugins {
    kotlin("jvm") version "1.8.20"
    id("org.jetbrains.compose") version "1.4.0"
}

dependencies {
    implementation(compose.desktop.currentOs)
}