0

Reading the following articles,

I can understand that we can use stateflow update() to update the value atomically.
This is useful when the updated value has a copy() or some long-running calculations. (API call, room data fetch, etc.)

My question is can we completely replace all the usages of stateflow.value using update() or is there any use-case where .value is the better choice?

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121

1 Answers1

1

I wouldn’t use it when it’s not necessary for the logic you want, because it’s more code and less performant. Also, if you put long-running suspend functions in there, then other coroutines that want to update it and are also using update will have to wait. If this doesn’t matter to your logic, you’ll be making everything slower.

I can't think of any reason to use it except when the new value must be dependent on the immediately previous value.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154