brabo-hi
01/17/2022, 10:51 PMdata 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 changesIan Lake
01/17/2022, 10:57 PMxxfast
01/17/2022, 11:37 PMMutableState<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 dispatcherscb
01/18/2022, 9:18 AMMutableState
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
.brabo-hi
01/18/2022, 7:07 PM