Hi guys, when using `StateFlow` .E.g ```val uiSta...
# compose
b
Hi guys, when using
StateFlow
.E.g
Copy code
val uiState = MutableStateFlow(MyUiState.Empty)
uiState.value = MyUiState.Loading
uiState.update { MyUiState.Loading }
What is the difference using
uiState.value
and
uiState.update
s
I think in this case there's no difference. You can read or write
value
atomically, but if you want a read+write to be one atomic operation you can use
update
. So if you do
flow.value = flow.value + 1
there's a chance that you get the wrong result if another thread modifies
flow.value
at the same time. With
flow.update { it + 1 }
you're guaranteed that flow's value gets increased by 1.
K 3
­čöą 2
Ô׼ 2
b
makes sens