Sean Proctor
10/17/2021, 4:00 PMLaunchedEffect
, but it seems that LaunchedEffect
gets canceled on recompose.
@Composable
fun TimeDisplay(viewModel: TimeDisplayViewModel) {
val properties: Map<String, Long> by viewModel.properties.collectAsState()
var timeRemaining by rememeber { mutableStateOf(0) }
Text(timeRemaining.toString)
val timeProperty = properties["time"]
LaunchedEffect(timeProperty) {
val currentTime = System.currentTimeMillis()
while (timeProperty > currentTime) {
timeRemaining = timeProperty - currentTime
delay(1000L)
}
timeRemaining = 0L
}
}
The LaunchedEffect
is always canceled during the delay. properties
is updated with an unrelated value.Stylianos Gakis
10/17/2021, 5:46 PMLaunchEffect
check for key equality? If it’s referential equality, timeProperty
seems to be re-calculated on every recomposition and that could cancel the previous LaunchedEffect
and launch a new one, meaning that the previous coroutine would be canceled. The first time it suspends and finds an opportunity to stop working is inside the delay
function.
Could you try replacing
val timeProperty = properties["time"]
with
val timeProperty by remember(properties) {
properties["time"]
}
And see if that helps?
This is a wild guess, but that’s what I’d try myself firstSean Proctor
10/17/2021, 5:53 PMLaunchedEffect
was canceled and not re-launched.Stylianos Gakis
10/17/2021, 6:08 PMtimeProperty
and currentTime
which both don’t seem to change inside the while loop. Shouldn’t the currentTimeMillis()
be taken again after the delay?Sean Proctor
10/17/2021, 6:11 PMStylianos Gakis
10/17/2021, 6:11 PM