2011-01-31 5 views
2

私のiPhoneアプリでNSUserDefaultsと働いています。何らかの理由で、設定の変更を有効にするためにアプリを2回起動/再開する必要があります。関連コード:だからここなぜiOS NSDefaultsを有効にするには2回の起動が必要ですか?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    ... 

    DLog(@"Registered default user defaults values."); 
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"DefaultUserDefaults" ofType:@"plist"]]; 
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; 

    ... 

} 

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    DLog(@"rescheduling notifications..."); 
    [self rescheduleAllNotifications]; 
} 

- (void)rescheduleAllNotifications { 
    // 
    // Wipe the slate clean by cancelling all local notifications 
    // 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

    // 
    // Only reschedule notifications if the user prefers to have them scheduled 
    // 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    if ([defaults boolForKey:@"notifications_enabled_preference"]) { 
     DLog(@"Notifications enabled. processing now..."); 

     ... 

は私のデバッガ出力(YESnotifications_enabled_preferenceセットから始まる)です。

[Session started at 2011-01-31 14:25:58 -0600.] 
AppDelegate application:didFinishLaunchingWithOptions:] Registered default user defaults values. 
AppDelegate applicationDidBecomeActive:] rescheduling notifications... 
AppDelegate rescheduleAllNotifications] Notifications enabled. processing now... 

-> Switch to Settings and turn notifications_enabled_preference to NO, then re-launch the app 

AppDelegate applicationDidBecomeActive:] rescheduling notifications... 
AppDelegate rescheduleAllNotifications] Notifications enabled. processing now... 


-> Click home screen button, then re-launch the app **again** 

AppDelegate applicationDidBecomeActive:] rescheduling notifications... 
AppDelegate rescheduleAllNotifications] Notifications disabled. Skipping notification processing. 

なぜそれを有効にするに変更した設定のために二回アプリを起動かかりますか?

+1

'-synchronize'を呼び出して値を正しく設定しようとしましたか? – kevboh

答えて

4

NSUserDefaultsは定期的(通常30秒程度)にディスクに自動的に同期します。この同期プロセスにより、ディスクへの変更を書き出すことができますが、ディスク上で行われた変更も反映されます。 -applicationDidBecomeActive:メソッドの一番上にある[[NSUserDefaults standardUserDefaults] synchronize]に電話をかけてください。

関連する問題