Marko Mitic
10/10/2019, 11:45 AMdelay()
)elizarov
10/10/2019, 11:50 AMMarko Mitic
10/10/2019, 11:57 AMDominaezzz
10/10/2019, 12:27 PMDennis Schröder
10/10/2019, 12:29 PMZach Klippenstein (he/him) [MOD]
10/10/2019, 12:55 PMdelay
is probably the most idiomatic way to do it with coroutines.
That said, if you're on Android, WorkManager might be more appropriate depending on what kind of objects you're managing (are they only in memory or are they stored in a db?) and how far in the future you're scheduling.Luis Munoz
10/10/2019, 3:00 PMelizarov
10/10/2019, 3:24 PMlaunch { delay(time); doSomething() }
is on the order of a few hundred bytes. Do it like this if that is Ok for you. Of course, a specialized solution would be more lighter-weight, but you’ll have to write it yourself if you want it to be optimized for your particular case.Luis Munoz
10/10/2019, 3:46 PMelizarov
10/10/2019, 3:53 PMDennis Schröder
10/10/2019, 3:58 PMZach Klippenstein (he/him) [MOD]
10/10/2019, 4:07 PMnwh
10/11/2019, 9:17 PMScheduledExecutorService
, just curious about kotlin solutionsDennis Schröder
10/11/2019, 9:24 PMsuspend fun scheduledPeriodicTask(
time: Long,
period: Long,
context: CoroutineContext = EmptyCoroutineContext,
task: suspend CoroutineScope.() -> Unit
): Unit =
withContext(context) {
delay(time)
while (true) {
task()
delay(period)
}
}
launch {
scheduledPeriodicTask(5000, 1000) {
println("bingo")
}
}
nwh
10/11/2019, 9:32 PMfun CoroutineScope.schedule()
so that the scope could be kept