Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUserDefaults write to disk?

I am writing an index for the currently selected UITabBar to [NSUserDefaults standardUserDefaults] but I am getting the odd situation where the index is not writen to disk. I am using

// TO WRITE
NSNumber *selectedTab = [NSNumber numberWithInt:[tabBarController selectedIndex]];
[[NSUserDefaults standardUserDefaults] setObject:selectedTab forKey:@"selectedTab"];

and ...

// TO READ
NSNumber *selectTab = [[NSUserDefaults standardUserDefaults] objectForKey:@"selectedTab"];
NSLog(@"SelectTab: %@", selectTab);
[tabController setSelectedIndex:[selectTab intValue]];

Can anyone tell me if setObject:forKey: does the actual write to disk, or do I need to force the write using something like synchronize ?

like image 590
fuzzygoat Avatar asked Apr 08 '26 02:04

fuzzygoat


1 Answers

You need to call synchronize, although it does get called automatically periodically (at least according to the documentation). A good time to call it is in the app delegate's applicationWillEnterBackground: and applicationWillTerminate:.

like image 61
Daniel Dickison Avatar answered Apr 09 '26 17:04

Daniel Dickison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!