Mark
05/28/2023, 11:20 AMList
of `Flow`s which I combine into a single Flow
using combine(Iterable)
. Now I want to add another Flow
to the List
. How to handle this? Keep a reference to the combine Job
and cancel/recreate accordingly? Combine the existing combine
Flow
with the new Flow
list item? Or perhaps represent the list as a Flow
and use runningFold
and flatMapLatest
? Something else?Odife K
05/28/2023, 6:49 PMMark
05/29/2023, 6:37 AMMutableList
of items where each item is represented by a Flow
. So I combine these `Flow`s into one Flow
. The items are being appended to as the user scrolls down and so at this point I need to recreate the combined flow. I’m just wondering the best way to do this.Odife K
05/29/2023, 8:51 AMMark
05/29/2023, 12:09 PMMutableList
with a MutableStateFlow
starting with an empty list, and using update
whenever I wanted to add a new item. Then I use flatMapLatest
and combine
to get the flow I need. Note: it’s important to not use combine
when the list is empty. Something like this:
val itemsStateFlow = MutableStateFlow(emptyList<Item>())
fun addItem(item: Item) {
itemsStateFlow.update { it + item }
}
val resultsFlow: StateFlow<Foo> = itemsStateFlow.flatMapLatest { items ->
if (items.isEmpty()) {
flowOf(Foo(emptyList()))
} else {
combine(items.map(Item::flow)) {
Foo(it)
}
}
}.stateIn(coroutineScope, SharingStarted.Eagerly, Foo(emptyList())