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

Brais Gabin

02/01/2021, 7:57 PM
Is there any way to get a Sharable Flow that ends? I have a slow producer with an end. And I have multiple slow consumers that need all the produced items. My current solution is convert that Flow in a List and then start the consumers. But I'm losing time because some consumers can start it's work as soon as the producer emits the first element.
m

Marc Knaup

02/01/2021, 8:36 PM
Catch the upstream completion with
onComplete { … }
and emit a special value downstream that signals the end of the stream. Downstream you can use
.takeWhile {}
to stop the downstream flow if the shared flow has emitted the special value.
4 Views