0

设置 MutableProperty 的值是否会导致生产者发出具有相同值的新事件?

换句话说,如果我不想要具有相同值的新事件,我是否需要这样做!= 检查?

let really = MutableProperty<Bool>(false)

func updateReality(newReality: Bool) {
    if really.value != newReality {
        really.value = newReality
    }
}
4

1 回答 1

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.

于 2017-10-19T12:23:01.263 回答