[ad_1]
I have a list of items that emits from a Flow
on a regular cadence of once every minute. Once this Flow
emits its list of items, each of those items has a list of images associated with it where each individual image can be in different states e.g. Placeholder, Loaded Image. This is where the second Flow
comes in.
So once the parent Flow
emits the items, I need to go and retrieve those images for each item. I want to kick off another Flow
to get those images. These images first emit
with their Placeholder states and then once they have successfully extracted the actual images, they can then emit
the Loaded Image.
Here is the code I have so far. I’m struggling to deal with how to make the child image flow actually trigger the parent flow to emit another value. As I first want it to set the state with all of the items with placeholders for their images and then one by one update the state once the images extract and get emitted by the child Flow
:
flowOfItems.map { items -> // This is a Flow
items.map { item ->
val images = repository.getImages(item) // e.g. 3 images
.map { extractImage(it) } // This is a list of Flows
Item(
item = item, // (Item)
images = images // (List<Image>)
)
}
}
.flowOn(Dispatchers.IO)
.collect {
withContext(Dispatchers.Main) {
_viewState.value = ScreenViewState(items = it)
}
}
Thanks in advance! I’d really appreciate any help as I’m very new to using Flows
and this is blocking me heavily.
[ad_2]