I want to have something occur at a 5 second tick....
# android
t
I want to have something occur at a 5 second tick. What is the kotlin/Android idiomatic way to do this? I’m currently doing:
Copy code
this.timer = Timer()
this.timer?.scheduleAtFixedRate(timerTask { simulate() }, 0L, 5000L)
I’m not a fan of this solution, because
this
isn’t what I expect this to be in that case. But when in rome, I guess? Or is there a different more modern idiomatic way to cause a real closure to tick at a rate?
t
I think the most idiomatic way to do something periodically is to use coroutines. Here is an example:
Copy code
scope.launch {
    while (true) {
        doSomething()
        delay(5000L)
    }
}