https://kotlinlang.org logo
Title
l

Luke Rohde

10/22/2019, 2:19 PM
is there a recommended way to schedule a periodic task with coroutines? i see
ticker
, but it’s marked as obsolete and it’s unclear why
m

marstran

10/22/2019, 2:34 PM
The docs say this:
Note: Ticker channels are not currently integrated with structured concurrency and their api will change in the future.
w

withoutclass

10/22/2019, 2:49 PM
Could use
repeat
with
delay
or a coroutine with a loop that checks
isActive
+
delay
👍 1
b

bdawg.io

10/22/2019, 3:57 PM
You could also use a
Flow
with an indefinite generator with a delay if you want it as a stream
d

Dennis Schröder

10/23/2019, 7:01 AM
suspend fun periodicTask(
    period: Long,
    context: CoroutineContext = EmptyCoroutineContext,
    task: suspend CoroutineScope.() -> Unit
) = withContext(context) {
    while (true) {
        delay(period)
        task()
    }
}
But I wouldn`t spawn to many of them like this.
u

uli

10/23/2019, 7:30 AM
why not?