user221256
user221256

Reputation: 435

Coroutines Handler like behaviour

In one part of my application I post runnable to a Handler, then the code inside run() executes and posts the same runnable to Handler with postDelayed(). This creates something like loop. Is this behaviour achievable with coroutines? I tried to use channel, but I couldn't get it to work.

Upvotes: 3

Views: 1989

Answers (1)

Marko Topolnik
Marko Topolnik

Reputation: 200138

Is this behaviour achievable with coroutines?

Yes, and in an almost embarrassingly straightforward way:

launch(UI) {
    while (!done()) {
        // loop body
        delay(loopDelayMillis)
    }
}

Upvotes: 2

Related Questions