Alexander Maryanovsky
05/26/2022, 8:41 AMString
describing that object, based on its properties, and then I want to display it somewhere in the UI. How should I do that?
I’m thinking of two options:
1. Make the function inline and collectAsState
all the properties it uses.
2. Make the function return a Flow
and then stateIn().collectAsState()
where I want to use it.
What do you think is better? Maybe there are other, better options?Zach Klippenstein (he/him) [MOD]
05/26/2022, 11:47 AMCasey Brooks
05/26/2022, 2:10 PMStateFlow
with a large data class
containing properties for everything you currently have as individual StateFlows, using state.update { it.copy(...) }
to make updates to it. I’ve found it to be much easier to work with and reason about when you have that single object holding all the state, and it’s a lot easier to split it out into individual streams (state.map { it.property }
) than it is to combine multiple streams into one.
Using individual StateFlows
assumes that they are independent streams of data, which I’ve found to rarely be the case. But when you have a single StateFlow, you can easily add properties for individual pieces of data, but you can also easily combine those properties and have derived values just by computing them in the class body. Not to mention if you need to update multiple properties at once, you will get a single atomic change, while individual StateFlows may have a period of time where the values are inconsistent as you update each one individually.Matthew Laser
05/26/2022, 3:50 PMAlexander Maryanovsky
05/26/2022, 4:26 PM