I have the following code whose purpose is to capture the NSUserDefaults change event for a specific key.
[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:SOME_NSSTRING_VARIABLE options:NSKeyValueObservingOptionNew context:NULL]; - (void)observeValueForKeyPath:(NSString *) keyPath ofObject:(id) object change:(NSDictionary *) change context:(void *) context {NSLog (@"Changed for key %@", keyPath); }
But watchValueForKeyPath will never be called. I even tried replacing SOME_NSSTRING_VARIABLE with the line specified in the Note about changing the NSUserDefaults key value , but that did not help.
Update: I am modifying NSUserDefaults from tabview. The above code for monitoring changes is on a different tab of the same tabview control. On the tab where I track changes (the tab where the code above exists), if I add:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];
the updated NSUserDefaults value is obtained correctly, but watchValueForKeyPath has never been called.
ios nsuserdefaults
Sankar
source share