0

Does setting the value of a MutableProperty result in the producer emitting a new event with the same value?

In other words, if I don't want new events with the same value, do I need to do this != check?

let really = MutableProperty<Bool>(false)

func updateReality(newReality: Bool) {
    if really.value != newReality {
        really.value = newReality
    }
}
ozool
  • 94
  • 1
  • 9

1 Answers1

0

Properties always send the new value, even if it's the same as the old value. It has to be this way or else you couldn't use it with non-Equatable types. But if you are dealing with an Equatable type, like Bool, then you can use the skipRepeats operator on your property to create a new property that only receives new values, and then expose that property to consumers:

let really = MutableProperty<Bool>(false)
let reallyWithNoRepeats = really.skipRepeats()

So you would update the value via really.value, but consumers subscribe to reallyWithNoRepeats to get new values only.

jjoelson
  • 5,771
  • 5
  • 31
  • 51