https://kotlinlang.org logo
Title
t

Travis Griggs

04/03/2020, 12:20 AM
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:
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

tseisel

04/03/2020, 8:20 AM
I think the most idiomatic way to do something periodically is to use coroutines. Here is an example:
scope.launch {
    while (true) {
        doSomething()
        delay(5000L)
    }
}