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

Sudhir Singh Khanger

08/05/2020, 9:26 AM
I fetch some data from the server and forward it to via a Flow object. I need to do some accessory work based on the fetched data but I don't need to transform the data which remains as it is. It doesn't look like I would use
map
operator here. Where should I do this accessory work? In the repository before the values are emitted or in the UI where the data is delivered. Or if there is another operator that let's me do such accessory work.
w

wasyl

08/05/2020, 9:31 AM
If by accessory work you mean just side effects, there’s
.onEach {  }
operator which receives each item for processing
As for where — it really depends on what it is (is it more of a data-layer concern or UI). Also there’s a question if you need to do that work before item gets emitted (that’s what
onEach
does) or in parallel. And if in parallel, then whether cancelling the flow should cancel this accessory work 🙂
s

Sudhir Singh Khanger

08/05/2020, 9:41 AM
Got it. I suppose I have to use
collect
which is where I can do the accessory work and also push the data to the UI using the
LiveData
. Thanks.
👍 1
4 Views