I have a bug submitted by a tester that if he performs an action and then reboots his phone (by pressing the home and Sleep/Wake button down for a few seconds) the app is not persisting state.
I have been able to reproduce this issue. [NSUserDefaults synchronize] is getting called but when I restart the app after the reboot, the value inside NSUserDefaults was not saved.
Does anybody know if synchronize stores to a buffer which is later saved to disk? If so, how do I flush the buffer (I thought synchronize was the same as a flush, but apparently not.)
(edit) In other words:
assert([[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY] == NO);
[[NSUserDefaults standardUserDefaults] setBool: YES forKey: MY_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
leave the app in the foreground and reboot the device after the above is called, then start the device back up and re-run the app. The assert should fire the second time around, but sometimes it doesn't.
To be very specific... I created a single view application and put the following code in the viewDidLoad
#define MY_KEY @"MY_KEY"
- (void)viewDidLoad
{
[super viewDidLoad];
BOOL key = [[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY];
NSLog(@"[[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY] == %@", key ? @"YES" : @"NO");
[[NSUserDefaults standardUserDefaults] setBool: !key forKey: MY_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"reboot now.");
}
Here is the output of three runs of the app:
2013-05-31 12:17:44.127 syncTest[162:907] [[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY] == YES
2013-05-31 12:17:44.133 syncTest[162:907] reboot now.
2013-05-31 12:18:49.771 syncTest[128:907] [[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY] == NO
2013-05-31 12:18:49.778 syncTest[128:907] reboot now.
2013-05-31 12:19:41.388 syncTest[124:907] [[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY] == NO
2013-05-31 12:19:41.397 syncTest[124:907] reboot now.
Note that the output was "YES, NO, NO" but it should have been "YES, NO, YES"