-
-
Notifications
You must be signed in to change notification settings - Fork 11
Description
I have a class that is making use of didSet on a property that is @DefaultsBacked. Unfortunately it seems like the current library behaviour is to not call didSet when the initial value is loaded and differs from the default. This leads to some quite subtle bugs.
It seems to me like the correct behaviour is to ensure that didSet is called whenever the value is set from UserDefaults.
Below is a fictional example to help explain the issue:
@ObservableDefaults(observeFirst: true)
class AudioState {
@DefaultsBacked
var muted = false {
didSet {
AudioController.toggleMuted(muted)
}
}
}In this example if muted is set to true in UserDefaults and then the app is launched the didSet is never called, even though it's value at some point changed from false to true. So the AudioController call will not happen until the setting is toggled off and on again in the UI which would then run the didSet handler correctly.
I think it's possible to work around this to some extent with a custom initialiser and then ensuring the same code gets run but it's quite awkward and error prone, especially if there are more than a couple of properties to handle.
Should this behaviour be changed at the library level to ensure the didSet handlers get called on init? Especially if its value differs from the default value.