I have an app that uses [NSUserDefaults standardUserDefaults] to store session information. Generally, this information is checked on app launch, and updated on app exit. I have found that it seems to be working unreliably in iOS 8.
I am currently testing on an iPad 2, although I can test on other devices if need be.
Some of the time, data written before exit will not persist on app launch. Equally, keys removed before exit sometimes appear to exist after launch.
I've written the following example, to try and illustrate the issue:
- (void)viewDidLoad
{
[super viewDidLoad];
NSData *_dataArchive = [[NSUserDefaults standardUserDefaults]
objectForKey:@"Session"];
NSLog(@"Value at launch - %@", _dataArchive);
NSString *testString = @"TESTSTRING";
[[NSUserDefaults standardUserDefaults] setObject:testString
forKey:@"Session"];
[[NSUserDefaults standardUserDefaults] synchronize];
_dataArchive = [[NSUserDefaults standardUserDefaults]
objectForKey:@"Session"];
NSLog(@"Value after adding data - %@", _dataArchive);
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Session"];
[[NSUserDefaults standardUserDefaults] synchronize];
_dataArchive = [[NSUserDefaults standardUserDefaults]
objectForKey:@"Session"];
NSLog(@"Value before exit - %@", _dataArchive);
exit(0);
}
Running the above code, I (usually) get the output below (which is what I would expect):
Value at launch - (null)
Value after adding data - TESTSTRING
Value after deleting data - (null)
If I then comment out the lines that remove the key:
//[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Session"];
//[[NSUserDefaults standardUserDefaults] synchronize];
And run the app three times, I would expect to see:
Value at launch - (null)
Value after adding data - TESTSTRING
Value after deleting data - TESTSTRING
Value at launch - TESTSTRING
Value after adding data - TESTSTRING
Value before exit - TESTSTRING
Value at launch - TESTSTRING
Value after adding data - TESTSTRING
Value before exit - TESTSTRING
But the output I actually see is:
Value at launch - (null)
Value after adding data - TESTSTRING
Value after deleting data - TESTSTRING
Value at launch - (null)
Value after adding data - TESTSTRING
Value after deleting data - TESTSTRING
Value at launch - (null)
Value after adding data - TESTSTRING
Value after deleting data - TESTSTRING
e.g. It seems to not be updating the value on exiting the app.
EDIT: I have tested the same code on an iPad 2 running iOS 7.1.2; and it appears to work correctly every time.
TLDR - In iOS 8 does [NSUserDefaults standardUserDefaults] work unreliably? And if so is there a workaround/solution?
iOS 8 introduced a number of behavior changes to NSUserDefaults
. While the NSUserDefaults
API has changed little, the behavior has changed in ways that may be relevant to your application. For example, using -synchronize
is discouraged (and always has been). Addition changes to other parts of Foundation and CoreFoundation such as File Coordination and changes related to shared containers may affect your application and your use of NSUserDefaults
.
Writing to NSUserDefaults
in particular has changed because of this. Writing takes longer, and there may be other processes competing for access to the application's user defaults storage. If you are attempting to write to NSUserDefaults
as your application is exiting, your application may be terminated before the write is committed under some scenarios. Forcefully terminating using exit(0)
in your example is very likely to stimulate this behavior. Normally when an application is exited the system can perform cleanup and wait for outstanding file operations to complete - when you are terminating the application using exit()
or the debugger this may not happen.
In general NSUserDefaults
is reliable when used correctly on iOS 8.
These changes are described in the Foundation release notes for OS X 10.10 (currently there is not a separate Foundation release note for iOS 8).
It looks like iOS 8 does not like setting strings in NSUserDefaults. Try encoding the string into NSData before saving.
When saving:
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:testString] forKey:@"Session"];
When reading:
NSData *_data = [[NSUserDefaults standardUserDefaults] objectForKey:@"Session"];
NSString *_dataArchive = [NSKeyedUnarchiver unarchiveObjectWithData:_data];
Hope this helps.
As gnasher729 said, don’t call exit(). There may be an issue with NSUserDefaults in iOS8, but calling exit() simply won’t work.
You should see David Smith’s comments on NSUserDefaults (https://gist.github.com/anonymous/8950927):
Terminating an app abnormally (memory pressure kill, crash, stop in Xcode) is like git reset --hard HEAD, and leaving
I found NSUserDefaults to behave nicely on iOS 8.4 when using a suite name to create an instance instead of relying on standardUserDefaults
.
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"MySuiteName"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With