@Ian Lake what is the difference between these two approaches
Copy code
data class Person(val name: String)
class PersonViewModel: ViewModel() {
private val _itemsFlow: MutableStateFlow<List<Person>> = MutableStateFlow(emptyList())
val itemsFlow: StateFlow<List<Person>> = _itemsFlow
private val _itemsState: MutableState<List<Person>> = mutableStateOf(emptyList())
val itemsState: State<List<Person>> = _itemsState
}
@Composable
fun PersonScreen(viewmodel: PersonViewModel = viewModel()) {
val itemsFromState: List<Person> by viewmodel.itemsState
val itemsFromFlow: List<Person> by viewmodel.itemsFlow.collectAsState()
}
they are all returning
List<Person>
and recomposed whenever data changes
✅ 1
i
Ian Lake
01/17/2022, 10:57 PM
You don't have to specifically mention anyone, there's plenty of people who can answer questions in the channel (and generally, you should try to avoid pasting more than a few lines of code in your original post - try to add code as a reply to your question so that it doesn't overwhelm the channel in general)
👍🏼 2
👍 4
Ian Lake
01/17/2022, 10:59 PM
But to answer your question, they'll both give you ~the same behavior in your UI layer
Ian Lake
01/17/2022, 11:00 PM
Obviously if you are getting your data from a database repository, those usually provide a Flow, which usually means you should just stay with a Flow up through each layer
x
xxfast
01/17/2022, 11:37 PM
MutableState<T>
is pretty limited in terms of doing further asynchronous operations like
.map
,
.flatMap
.reduce
, and you cant easily cross the thread boundaries with different dispatchers
🙏 1
c
cb
01/18/2022, 9:18 AM
MutableState
has never been described as a reactive stream. If you need that behavior then
Flow
is your best option.
However, you can quite easily perform suspending operations like that in a
LaunchedEffect
.
➕ 1
b
brabo-hi
01/18/2022, 7:07 PM
i think the best option would be to expose a stateflow and collect it as state