当应用程序从后台恢复时,不会发送NSUserDefaultsDidChangeNotification

时间:2013-06-25 21:02:08

标签: ios nsnotifications

Apple的文档说“系统将许多相关通知排队,并在它再次开始执行代码时(在前台或后台)将它们传送到应用程序”,包括NSUserDefaultsDidChangeNotification,但我的应用程序没有收到它。以下是相关事件的日志记录:

2013-06-25 16:23:36.857 -[DPLAppDelegate applicationWillResignActive:] [debug]: end willResignActive
2013-06-25 16:23:36.859 -[DayViewController handlePreferenceChange:] [debug]: day controller received user defaults did change notification
2013-06-25 16:23:36.970 -[AppDelegate applicationDidEnterBackground:] [debug]: end didEnterBackground

// Here I go into Settings app and change something that should trigger userdefaultsdidchange

2013-06-25 16:23:52.043 -[AppDelegate applicationWillEnterForeground:] [debug]: end willEnterForeground
2013-06-25 16:23:52.045 -[AppDelegate applicationDidBecomeActive:] [debug]: end didBecomeActive

我认为这是相关的代码:

AppDelegate.m

- (void)applicationDidEnterBackground:(UIApplication *)application 
{
// save our last active day
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:kLastActiveTimeDefault];
[[NSUserDefaults standardUserDefaults] synchronize];

DLog(@"end didEnterBackground");
}

DayViewController.m

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter]
        addObserver:self selector:@selector(handlePreferenceChange:) name:NSUserDefaultsDidChangeNotification object:nil];
}

- (void)handlePreferenceChange:(NSNotification *)note
{
    DLog(@"received user defaults did change notification");
    // do stuff
    [self updateDisplay];
}

应该发生的是用户在“设置”中进行了更改,该设置发布了NSUserDefaultsDidChangeNotification,后者将发送到-handlePreferenceChange,后者会更新某些数据并刷新显示。

但NSUserDefaultsDidChangeNotification在恢复时没有发生。我可以看到我订阅了通知,因为它是由我的簿记更新触发到kLastActiveTimeDefault -willResignActive:

我已经尝试将lastActiveTime的簿记注释掉,以防通知合并功能被搞砸了,但它似乎没有改变任何东西。那么这是什么一回事? Apple非常清楚iOS应该是在我的应用程序处于睡眠状态时通过设置更改为我保存通知,但它没有发生。

0 个答案:

没有答案