Skip to content

Instantly share code, notes, and snippets.

View xxhdpi's full-sized avatar

Jetfly xxhdpi

View GitHub Profile
@xxhdpi
xxhdpi / CoroutineTimer.kt
Created April 11, 2021 11:16 — forked from gmk57/CoroutineTimer.kt
Coroutine-based solution for delayed and periodic work
/**
* Coroutine-based solution for delayed and periodic work. May fire once (if [interval] omitted)
* or periodically ([startDelay] defaults to [interval] in this case), replacing both
* `Observable.timer()` & `Observable.interval()` from RxJava.
*
* In contrast to RxJava, intervals are calculated since previous run completion; this is more
* convenient for potentially long work (prevents overlapping) and does not suffer from queueing
* multiple invocations in Doze mode on Android.
*
* Dispatcher is inherited from scope, may be overridden via [context] parameter.