0

In vc1 im setting a BOOL [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"myKey"];

In vc2 I want to observe if this BOOl changes [[NSUserDefaults standardUserDefaults] boolForKey:@"myKey"]

Checked here Add Observer to BOOL variable and Registering a bool for a NSNotification

neither seemed to fit my pattern

Community
  • 1
  • 1
JSA986
  • 5,870
  • 9
  • 45
  • 91

2 Answers2

1

Use below code it will call when your BOOL value changed.You can use KVO to observe the value of your myKey .Write this in any class where you observe the values.Write it in viewDidLoad in vc2

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults addObserver:self
           forKeyPath:@"myKey"
              options:NSKeyValueObservingOptionNew
              context:NULL];

Implement this method in your class where you have added observer in vc2

    -(void)observeValueForKeyPath:(NSString *)keyPath
                     ofObject:(id)object
                       change:(NSDictionary *)change
                      context:(void *)context
{

    //check here if it is myKey than do something
    if([keyPath isEqualToString:@"myKey"]){
        //do your work
    NSLog(@"KVO: %@ changed property %@ to value %@", object, keyPath, change);
    BOOL newValue = [[change objectForKey: NSKeyValueChangeNewKey] boolValue];
    }
    else{
        NSLog(@"Not my key");
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

    }
}

Remove observer in vc2 dealloc

-(void)dealloc{

    [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:@"myKey"];
}

In observeValueForKeyPath check for if value changed for myKey than do whatever you want to.

codester
  • 36,891
  • 10
  • 74
  • 72
  • 1
    This is blatantly incorrect. You don't set properties on `NSUserDefaults`. Please don't run after rep. – duci9y Jul 28 '14 at 22:00
  • @duci9y why this is incorrect have you used it??I have used it and wotking perfect??? – codester Jul 28 '14 at 22:04
  • observeValueForKeyPath:ofObject:change:context should call super if the key isn't one you're interested in – iain Jul 28 '14 at 22:08
  • `NSUserDefaults` is not KVO compliant. That it works for you is merely by chance, the functionality might break in a future update and you'd be none the wiser. – duci9y Jul 28 '14 at 22:09
  • @duci9y You are wrong than it is fully KVO complaint and you can observe the value. – codester Jul 28 '14 at 22:11
0

Register for the NSUserDefaultsDidChangeNotification and check your user defaults whenever the notification fires to see if the value that changed is the one you're interested in.

duci9y
  • 4,128
  • 3
  • 26
  • 42
  • You can not get which keyname changed with this – codester Jul 28 '14 at 22:12
  • "This notification does not contain a userInfo dictionary." Even worse, the only specification of when NSUserDefaultsDidChangeNotification is posted says that it is posted "when a persistent domain is changed" – codester Jul 28 '14 at 22:18
  • it will notify you when not value changed when it writes to the specific persistent domain and second you can not able to find which key you have changed i think this should require as there are so many keys in user defaults and it may have different values.You will not get this information from `NSUserDefaultsDidChangeNotification` – codester Jul 28 '14 at 22:40
  • @codester Who said it only fires when changed are persisted to disk? – duci9y Jul 28 '14 at 22:42
  • not persisted but change in `persitent domain` docs:https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSUserDefaults_Class/Reference/Reference.html – codester Jul 28 '14 at 22:43
  • @codester So? I cannot understand what the problem is. – duci9y Jul 28 '14 at 22:44
  • Problem is you when you set `key1` to YES and `Key2` to NO you will not get to know which key is changed and to what value.This notification only notifies anything in NSUserDefaults has changed but you will not get what has chaged. – codester Jul 28 '14 at 22:52