acando86
12/13/2018, 1:08 PMDico
12/13/2018, 1:42 PMacando86
12/13/2018, 2:14 PMfor (newData in channel) {
heavyComputation(newData)
// This process is expensive, I want to keep receiving newData as soon as they are available, cancel the heavy computation and start a new one with fresh data
}
acando86
12/13/2018, 2:15 PMDico
12/13/2018, 2:22 PMfun CoroutineScope.heavyComputation(data)...
val parent = SupervisorJob()
val scope = new coroutine scope with job as context
for (data in channel) {
parent.cancelChildren()
scope.heavyComputation(data)
}
That seem right to you?Dico
12/13/2018, 2:23 PMlaunch
or async
or whatever, make sure you also have an appropriate dispatcher in the scopeDico
12/13/2018, 2:26 PM