Yes, I also experience this, and it seems to be a mistake, below is a short workaround that I am currently using until it is fixed. Hope this helps!
Also, to clarify, since iOS 7 KVO works great with NSUserDefaults, and this is undoubtedly the key value observed, as Matt said, it is explicitly written in the NSUserDefaults.h in the iOS 9.3 SDK: "NSUserDefaults can be observed using the Key Note value for any key stored in it. "
#include <mach/mach.h> #include <mach/mach_time.h> @property uint64_t newTime; @property uint64_t previousTime; @property NSString *previousKeyPath; -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { //Workaround for possible bug in iOS 9.3 SDK that is causing observeValueForKeyPath to be called multiple times. newTime = mach_absolute_time(); NSLog(@"newTime:%llu", newTime); NSLog(@"previousTime:%llu", previousTime); //Try to avoid duplicate calls if (newTime > (previousTime + 5000000.0) || ![keyPath isEqualToString:previousKeyPath]) { if (newTime > (previousTime + 5000000.0)) { NSLog(@"newTime > previousTime"); previousTime = newTime; NSLog(@"newTime:%llu", newTime); NSLog(@"previousTime:%llu", previousTime); } if (![keyPath isEqualToString:previousKeyPath]) { NSLog(@"new keyPath:%@", keyPath); previousKeyPath = keyPath; NSLog(@"previousKeyPath is now:%@", previousKeyPath); } //Proceed with handling changes if ([keyPath isEqualToString:@"MyKey"]) { //Do something } } }
source share