i have an app that runs on a kiosk. I basically ha...
# getting-started
c
i have an app that runs on a kiosk. I basically have this code, that works fine. BUT, in debug mode I can change the delayInterval. But when it changes, I have to wait for the original interval to hit, before the new interval is picked up. How would you go about "interupting" the delay()?
Copy code
//start reoccruing task on kiosk
    viewModelScope.launch {
      while (true) {
        delay(state.delayInterval)
        syncUseCase.sync()
      }
    }
fwiw, I'm assuming it would be saving a reference to this "job" and then cancelling it, and restarting it? Or maybe I shouldn't use my viewModelScope and just create a different one? Maybe another trick I'm missing?
s
Put the
delayInterval
in a
StateFlow
and use
collectLatest
on it.
Copy code
val delayInterval = MutableStateFlow(someDelay)

viewModelScope.launch {
  delayInterval.collectLatest { interval ->
    while(true) {
      delay(interval)
      syncUseCase.sync()
    }
  }
}
☝️ 1
c
ooooh.
delayInterval is actually a compose snapshot state, so I think I can convert that using an extension method
instead of refactoring that type
cool
Copy code
snapshotFlow { state.delayInterval }.collectLatest { interval ->
will test that now!
ooooh yeah. THANK YOU @Sam
s
🍻
j
That's awesome, thanks for sharing!