Hi all 👋 ,
How to combine and collect multiple flows in a type safe way?
Current code
combine (flow1, flow2, flow3, ...) { data1, data2, data3, ... ->
DataStructure(data1, data2, data3, ...) // DataStructure would be Pair(), Triple, etc depending on number of flows
}.collectLatest { (data1, data2, data3, ...) ->
// Can use all data in type safe way
}
As the above code requires
DataStructure
to be modified for any flow addition or removal from the
combine
, and also to create new custom
DataStructure
for more than 3 data.
How can I change this in a type safe way?
combine (flow1, flow2, flow3, ...) { dataList ->
dataList
}.collectLatest { dataList ->
// Can NOT use all data in type safe way, the data type is Array<Any?>
}
This code loses the type safety as the data list is
Array<Any?>
.