I have this code in my AppDelegate
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"+applicationDidBecomeActive");
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(defaultsChanged:) name:NSUserDefaultsDidChangeNotification object:nil];
NSLog(@"-applicationDidBecomeActive");
}
- (void)defaultsChanged:(NSNotification *)notification {
NSLog(@"+defaultsChanged");
// Get the user defaults
// NSUserDefaults *defaults = (NSUserDefaults *)[notification object];
NSLog(@"-defaultsChanged");
}
applicationDidBecomeActive is fired only once, when the application is launched.
If defaults are not changed, defaultsChanged is not fired.
But, if defaults are changed defaultsChanged is fired twice, one after another.
I can not understand why. Can anyone help me? Thank you.
Are you quite certain that you only changed ONE thing in the defaults? If you change multiple defaults, then
NSUserDefaultsDidChangeNotificationwill be sent multiple times. I use a similar method in some of my apps, and I have never encountered this error. This makes me inclined to think that multiple defaults must have been changed.