0

I have a usecase that i combine two flows and convert it to stateFlow by using statIn. When user clicks on the list, i want to update current ui state and pass it to my stateflow. Since it's not MutableStateFlow, i am not able to update it. The reason i dont use MutableStateFlow is i want to get benifit of using "WhileSubscribed".

The sample code is like below :

val state = getList().catch {      // This flow emits every 5 seconds.
  ..... 
}.combine(getSecondList().catch { 
  .....
}) { firstList, secondList ->
  .....
  .....
  UiState.Success(firstList, secondList)
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(FLOW_TIMEOUT), UiState.Loading)


fun updateList {                   // User clicks on list.
  .....
  .....
  val state = (state.value as? UiState.Success)
  val newState = state?.copy(updatedFirstList, updatedSecondList)
  // Want to update state value here.
}

Is there any other way to achive what i want to do? Appreciate any help.

Berkay Kireçci
  • 651
  • 5
  • 18
  • If there are two separate sources for the state, then what should happen if both of them would like to change it? What if you call `updateList()`, but 1ms later there is a new item in one of source flows? – broot Aug 20 '23 at 17:48

0 Answers0