Jesse Hill
10/18/2021, 4:51 PM+ instead of creating a MutableList, adding all the values and then changing the MutableList to a List with toList? Code in 🧵 .Jesse Hill
10/18/2021, 4:55 PMcombine(
repository.getAllTicketsOfType(),
repository.getAllTicketsOfOtherType()
) { firstTickets, secondTickets ->
val list: MutableList<TicketDetails> = mutableListOf()
list.addAll(firstTickets)
list.addAll(secondTickets)
list.toList()
}.collect {
it.forEach { ...do something with tickets }
}
Example 2:
combine(
repository.getAllTicketsOfType(),
repository.getAllTicketsOfOtherType()
) { firstTickets, secondTickets ->
firstTickets + secondTickets
}.collect {
it.forEach { ...do something with tickets }
}Orhan Tozan
10/18/2021, 4:59 PMLuke
10/18/2021, 5:21 PMbuildListPaul Griffith
10/18/2021, 5:28 PMsequence block (I don't think you need a flow here at all)
sequence {
yieldAll(repository.getAllTicketsOfType())
yieldAll(repository.getAllTicketsOfOtherType())
}.forEach { ticket ->
TODO("Something with tickets")
}Jesse Hill
10/18/2021, 5:54 PMflow here is because the repository returns Flow<List<TicketType>> so I think I need combine to put them into a single List when the flow is collected. I tried out the sequence as you suggested but yieldAll expects Iterator, Iterable, or Sequence so doing repository.getAllTicketsOfType() didn’t work because it returns a flow. That is a good reminder about sequence though, I keep forgetting about it 😂Paul Griffith
10/18/2021, 5:58 PMflow builder that might work (very similar to sequence :
flow {
emitAll(repository.getAllTicketsOfType())
emitAll(repository.getAllTicketsOfOtherType())
}.collect { ticket ->
TODO("Do something with each ticket")
}Michael Böiers
10/19/2021, 7:37 AMsequenceOf(list1, list2).flatten().forEach { … } (for the initial example of processing multiple lists of course, this doesn’t work for flows)Klitos Kyriacou
10/19/2021, 8:07 AMlist.toList() returns a copy of the list, which is unnecessary - you could just return list as a List (which MutableList extends) so that there is no copying.