https://kotlinlang.org logo
#coroutines
Title
# coroutines
t

Tolriq

11/12/2020, 9:08 AM
Was wondering if there some more idiomatic ways to accomplish :
Copy code
fun scheduleRefresh() {
	job?.cancel()
	job = launch {
		while (isActive) {
			val newRefreshTime = getARefreshTime()
			if (newRefreshTime == 0) {
				break
			}
			delay(newRefreshTime)
			doSomething()
		}
	}
}
g

gildor

11/12/2020, 9:15 AM
scheduleRefresh can be Flow with stream of refresh events, so you can apply mapLatest on it
👍 1
t

Tolriq

11/12/2020, 9:27 AM
I'm not sure I see all the part
n

Nick

11/12/2020, 2:27 PM
can you use a
ticker
?
t

Tolriq

11/12/2020, 2:37 PM
The delay is dynamic, the code works, just wondering if there was better way to handle such dynamic pooling scenario
k

kevin.cianfarini

11/12/2020, 7:08 PM
if the part that seems unintuitive to you is the manual cancellation of the job, you could make the variable that houses the job
Delegates.observable
and cancel the prior job every time a new value is set.
3 Views