is there a recommended way to schedule a periodic ...
# coroutines
l
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
The docs say this:
Copy code
Note: Ticker channels are not currently integrated with structured concurrency and their api will change in the future.
w
Could use
repeat
with
delay
or a coroutine with a loop that checks
isActive
+
delay
👍 1
b
You could also use a
Flow
with an indefinite generator with a delay if you want it as a stream
d
Copy code
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
why not?