https://kotlinlang.org logo
#flow
Title
# flow
s

sjj

05/24/2021, 11:30 PM
Hey folks 👋. I have a use-case where I construct a Flow that uses resources that can be expensive to initiate (e.g. queries from a database) but eventually completes (is not a continuous stream of data). I was hoping that I could use the
shareIn
operator to ensure the flow is only actually collected once, and subsequently cached. Based on what I've read, due to the fact that
collect
on a
SharedFlow
never actually returns, the recommended pattern is to use some kind of sentinel value to denote the completion of data and combine that with the
takeWhile
operator. Just wanted to check this is indeed the recommended solution.
d

dshevliakov

05/25/2021, 4:30 PM
Do you really need Flow if it is not "continuous stream of data"? Anyway, I believe you could collect your Flow with first()/single()/take(1) operator, so it would collect first item and then complete.
s

sjj

05/26/2021, 12:03 AM
Hi @dshevliakov while it's not a never-ending stream of data, it's still a stream of data... curious what would be the alternative if not using Flow and also not wanting to wait for processing until all elements have been consumed?
d

DALDEI

10/29/2021, 2:16 AM
sequence ?
2 Views