https://kotlinlang.org logo
#flow
Title
d

Daniel

10/23/2020, 11:46 AM
I have a MutableStateFlow which contains a HashMap. I want to insert a new value into the hashmap, but only if the prexisting key hasn't changed since I started computing the new value. Like so, except I think the following has race conditions
Copy code
val cached = state.value[key]
val update = computeUpdate()

if (state.value[key] == cached) {
  val mutable = state.value.toMutableMap()
  mutable[key] = update
  state.value = mutable.toMap():
}
EDIT: I also want to ensure that if other keys have been changed in the meantime this function doesn't overwrite them with their previous values
3 Views