hello, which one is better to udapte StateFlow as ...
# coroutines
p
hello, which one is better to udapte StateFlow as a UI state private val _uiState: MutableStateFlow<State> = _MutableStateFlow_(initialState) #A
Copy code
viewModelScope.launch {
     _uiState.update(newState)
 }
#B
Copy code
_uiState.update(newState)
#C
Copy code
_uiState.value = newState
🅱️ 2
n
Doesn't matter, you can update from it from any context. Use the most readable variant, it can be even
tryEmit
, but I personally prefer 'C' variant
🙌 2
if it's a state with state flow, it will not suspend your execution
🙌 1
x
I’d stick with #B for most of the cases, as it uses
synchronized
to resolve concurrent state updates. Take a look at the source code for *`MutableStateFlow<T>.update` here* You could end up with race conditions when using #C There’s no need for a coroutine to update the state, option #A introduces unnecessary overhead
🙌 1
u
For #B, did you mean:
Copy code
_uiState.update { previousState -> newState }
I think, there is no advantage of #B over #C as long as you are not referencing previousState