Re: observeValueForKeyPath called twice when changing NSUserDefaults value
Re: observeValueForKeyPath called twice when changing NSUserDefaults value
- Subject: Re: observeValueForKeyPath called twice when changing NSUserDefaults value
- From: Quincey Morris <email@hidden>
- Date: Sun, 13 Nov 2016 13:24:44 -0800
- Feedback-id: 167118m:167118agrif8a:167118sYHtXHasMg:SMTPCORP
On Nov 13, 2016, at 13:00 , Steve Mills <email@hidden> wrote:
>
> I've added an observer for an NSUserDefaults keyPath.
Is this a new thread, or a continuation of the one we had a couple of weeks ago (IIRC)?
NSUserDefaults is not documented as KVO compliant for any of its properties. You really shouldn’t try to observe them. Or are you really observing NSUserDefaultsController.values? That is KVO compliant, but is going to be quirky like all those NS…Controller objects.
Since this issue keeps coming up, it looks like there was a behavioral change in 10.12 that may well be a minor bug. But there’s no API contract about minimality of notifications, so you don’t have much leverage.
Whatever you’re going to observe (user defaults or anything else), I’d recommend you use the following practices:
— Don’t use the new and old values in the change dictionary. Use the current value of the property instead. If you need to compare this to the last value you saw, keep the last value as a custom property within whatever object is handling the notification. (Current vs. last-seen is not the same as new vs. old change value, but it’s usually what you really want anyway.)
— If the frequency of changes is low, don’t bother suppressing multiple updates. If the frequency is higher, compare current vs. last-seen and suppress updates where there’s no real change.
You’d only violate these principles if you were forced to, for example if the frequency of updates is so high that there would be a performance problem accessing the current value.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden