I am currently attempting to refresh data from a s...
# compose
d
I am currently attempting to refresh data from a server at a regular interval, only when the UI is displayed. So when back or home is pressed, the checks will stop. When I return, it will continue again. I thought I could use a LaunchedEffect, as it runs in a CoroutineScope. I thought that this would result in the Coroutine automatically cancelling when going home. However, I am not sure how it is linked to the lifecycle. It remains active in the background.
Copy code
LaunchedEffect(true) {
    while (isActive) {
        delay(60000L)
        vm.getData()
    }
}
Is there a better pattern to use in this case?
a
This will remain active in the background, yes. There are a few new APIs in flight for
androidx:lifecycle:lifecycle-ktx
that will look more like:
Copy code
LaunchedEffect(true) {
  repeatOnLifecycle(Lifecycle.State.STARTED) {
    while (true) {
      delay(...)
      vm.getData()
    }
  }
}
d
Ahhh, that will be great. I look forward to it. 👍
👍 1
d
For other interested parties, it was added in
2.4.0-alpha01
. I didn't notice a new alpha version.