Hi! Noob question here :sweat_smile: I’m currently...
# compose-desktop
a
Hi! Noob question here 😅 I’m currently learning compose directly in desktop (maybe not the most clever idea…). I’m able to code simple UIs, but when it’s time to code “real” apps I’m struggling with suspend functions How should I bridge my suspended code and Compose? My code in the thread
Currently, I’m doing
Copy code
suspend fun fetchContent(): String {
    println("Loading...")
    delay(2000)
    println("Done!")
    return "Hello world from suspend"
}

fun main() = Window {
    val coroutineScope = rememberCoroutineScope()
    var (content, setContent) = remember { mutableStateOf("Loading...") }
    coroutineScope.launch {
        setContent(fetchContent())
    }
    Text(content)
}
Which not only looks ugly as hell, it’s also invoking fetchContent twice (I can see it in stdout)
s
You probably want to wrap your content fetching inside the LaunchedEffect plus some key instead of a coroutineScope.launch: https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#launchedeffect_1
1
See also here, which has a bit more explanation: https://developer.android.com/jetpack/compose/lifecycle#launchedeffect
❤️ 1
a
I’ll take a look! Thanks!! 😄
Works like a charm, thanks again!
🎉 1
z
Also see
produceState
if you’re using a
LaunchedEffect
to update a
MutableState
👍 1