Bruno Ortiz
11/08/2020, 3:18 PMfun receive(interval: Duration) = flow {
while (true) {
delay(interval.toMillis())
val messages = getMessages(interval)
messages.forEach {
println("Emitting $it")
emit(it)
}
}
}
I can consume the flow "forever" like this:
receive(Duration.ofSeconds(1))
.collect {
println(it)
}
Or limit the number of messages that i consume:
val amount = 10
receive(Duration.ofSeconds(1))
.take(amount)
.collect {
println(it)
}
I tested this and the flow "completes", but i do not understand what happens in the second case, do the while(true)
loop gets suspended forever? Is this a problem?Adam Powell
11/08/2020, 3:24 PMAbortFlowException
(a subclass of CancellationException
) from your receive
flow's call to emit
to perform cleanup.Bruno Ortiz
11/08/2020, 3:36 PMBruno Ortiz
11/08/2020, 3:40 PMreceive
flow. I think i got it.
Thanks!