How do I call a suspended function once during eac...
# compose
m
How do I call a suspended function once during each composition? e.g.
Copy code
@Composable
fun SomePage() {
    val body = remember {   // Do I need to put this in remember to only call it once?
        suspendedGetData()  // This should only be called once per composition of SomePage
    }

    Text(body)
}
c
Use
produceState
to fetch the data
Copy code
@Composable
fun SomePage() {
    val body: Response? by produceState<Response?>(null) {
        value = suspendedGetData()
    }

    Text(body)
}
z
Do you want to load the data on every recomposition, or only the first time it’s composed?
m
And I'd want to load the data every recomposition as it may change during the lifetime of the application
z
If it can change, then you should be subscribing to some sort of signal that it changed - either a callback, rx observable, flow, mutable state, etc. If you only reload on recomposition it won’t do what you want because: 1) nothing guarantees your composable will be recomposed when the source data changes, and 2) the composable can recompose for many reasons without anything having changed, so you could end up doing way more reloads than you need to.
m
Hmm, I guess in that case I'll open an issue to get that on routing-compose