I'd lke to hear peoples comments on what the best practices would be for the need to checking a condition before updating a User Interface element in iOS (including watchOS and tvOS).
Here are some examples *
1. An Undo button keeping track of changes
func update(_ data: Data) {
updateHistory(data: Data)
if !btnUndo.isEnabled { btnUndo.isEnabled = true } // use this one or
btnUndo.isEnabled = true // use this one?
}
2. Text in a label that hasn't changed
var timerMonitorHeartRate = Timer()
func startMonitoring() {
timerMonitorHeartRate.invalidate()
timerMonitorHeartRate = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] timer in
self?.displayHeartRate()
}
}
func displayHeartRate() {
let heartRateValue = BLEHeartRate.value // value comes from some async function
let heartRateString = String(format: "Heart Rate %1.2f", heartRateValue)
if (lblHearRate.text != heartRateString) { lblHearRate.text = heartRateString } // use this one or
lblHearRate.text = heartRateString // use this one?
}
* these exmples are just off the top of my head, I didn't check them for syntax, only to give you an idea of what I'm talking about.
Should we check the conditon before updating an item or does the underlaying code do that for us (i.e., the button will not actually undergo a paint refresh since it will internally check and already know that it is enabled). If this is the case, would the same thing happen when updating a text property with the same text?