getsling / GVUserDefaults

NSUserDefaults access via properties.
MIT License
952 stars 92 forks source link

Observing changes to GVUserDefaults property values #10

Closed sibljon closed 10 years ago

sibljon commented 10 years ago

As far as I can tell, there is no support for observing property value changes via NSNotificationCenter or KVO (or any other method like block-callbacks, for that matter). What are your thoughts on the prospect of this feature?

kevinrenskers commented 10 years ago

All the properties are of course stored in NSUserDefaults so all normal observing patterns simply work. That said, having a dead easy, block based change notification system would be cool.

On 18 Dec 2013, at 22:26, Jonathan Sibley notifications@github.com wrote:

As far as I can tell, there is no support for observing property value changes via NSNotificationCenter or KVO. What are your thoughts on the prospect of this feature?

— Reply to this email directly or view it on GitHub.

sibljon commented 10 years ago

AFAIK, observing NSUserDefaultsDidChangeNotification is the best way to observe NSUserDefaults, and it doesn't provide any information about specific updates. That's the main disadvantage.

I think a light-weight (and small code foot print) solution would be calling willChangeValueForKey: and didChangeValueForKey: when GVUserDefaults setters are used. However, these two calls need to be made in every single GVUserDefaults generic setter (i.e. doubleSetter, objectSetter, etc), right? Or can you think of a more centralized place to make the KVO calls?

kevinrenskers commented 10 years ago

Back from my vacation, I'll look into this soon.

kevinrenskers commented 10 years ago

As far as I can see, this already works fine without any code changes.

- (void)viewDidLoad {
    [super viewDidLoad];
    [[GVUserDefaults standardUserDefaults] addObserver:self forKeyPath:@"userName" options:NSKeyValueObservingOptionNew context:nil];
    [GVUserDefaults standardUserDefaults].userName = @"Test";
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSLog(@"keyPath: %@", keyPath);
}
sibljon commented 10 years ago

Interesting! I apologize for not having tested it... apparently my understanding of the implementation wasn't correct! Thanks for following up, and I hope you had a nice vacation.

sibljon commented 10 years ago

Just to follow up: KVO on GVUserDefaults is working perfectly in my testing. — Sent from Mailbox for iPhone

On Tue, Jan 14, 2014 at 6:15 AM, Kevin Renskers notifications@github.com wrote:

Closed #10.

Reply to this email directly or view it on GitHub: https://github.com/gangverk/GVUserDefaults/issues/10

kevinrenskers commented 10 years ago

Awesome :)