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

taer

02/02/2021, 10:12 PM
Slightly different question from yesterday. I have a flow that can only be consumed once(a grpc stream). I need to pull the first element off like with
first
but I don't want to close the flow. I need the rest of the elements to be still available.
t

taer

02/02/2021, 10:19 PM
Something more like this
Copy code
val requests: Flow<Request>

val foo = requests.toList()
val targetServer = doSomethingWithFirstElement(foo.first())
targetServer.makeReuqest(foo.asFlow())
but without pulling the flow into memory
t

Tijl

02/02/2021, 10:28 PM
you can do
shareIn
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/share-in.html then collect the
SharedFlow
once with
first
, and another time with a filter to omit the first item.
t

taer

02/02/2021, 10:31 PM
I tried that. The shareIn got greedy and drained the flow. my second call the flow only contained the last element. I had set it up w/ a replay of one.
putting some logging around it to see what's going on. I kinda want that buffer(0) behavior with one replay
t

Tijl

02/02/2021, 10:39 PM
if you don’t want to buffer it then what you want is very specific, so you should just collect yourself (once) and save the first element for whatever way you want to get it, and then emit the other elements to a new flow, then you can suspend again on your emit(inside your collect) with the desired behaviour (no buffer as you just said)
2 Views