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"
i had gone through the same problem and i identify that i had two reference of [NSUserDefaults standardUserDefaults] object on different classes because nsdefault is a singleton class soo the one called it earlier will have it if u might had already reference of [NSUserDefaults standardUserDefaults] try to finish it from the first reference and let the instance to be free from there and then call the instance at your other class and synchronize hopefully will be done
In my case, some values I wrote to the NSUserDefaults did persist and some did not. After lots of searching, I noticed the the key-value pair that did not save had a key that was capipatilized, i.e. TheKey v.s theKey. When I changed my key to a camel-cased key (theKey instead of TheKey), the value did persist thru app quit and re-launch. It seems very weird to me why that would be, but it worked in my case.
Related
I have some code where I am reading a switch value from my settings bundle however on some instances it reads the wrong value.
This is what my code looks like, it's in my app delegate applicationDidBecomeActive
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
NSNumber *branchString = [settings objectForKey:#"signout"];
BOOL userSignOut = [[settings objectForKey:#"signout"] boolValue];
NSLog(userSignOut ? #"Yes" : #"No");
What happens is when the app first loads I receive NO then when I switch to settings and turn my switch over to YES, when I log back in I still get NO however if I then switch between settings and my app again with out touch the switch in settings that's when I get YES even though it should have happened a step earlier.
Are you using this when setting the value?
[[NSUserDefaults standardUserDefaults] setObject:(id) forKey:(NSString *)];
[[NSUserDefaults standardUserDefaults] synchronize];
You can use NSUserDefaultsDidChangeNotification as observer.
I have an app which shows the settings page ONCE! This is when it is first download from the app store. After that then it goes to the main page only.
but when you swipe the app when you double click the iPhone button and remove the app then it goes back to the settings page.
Here is some code from my app
didfinishwithOptions
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"SettingsShown"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"SettingsShown"];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:#"ShowBackButton"];
[[NSUserDefaults standardUserDefaults] synchronize];
self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"SettingsViewController"];
}
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"SettingsShown"] == NO)
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:#"SettingsShown"];
}
then I have put
- (void)applicationWillTerminate:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:#"SettingsShown"];
NSLog(#"Application is terminated");
}
this should run when you swipe the application when you double click on the iPhone button. but it isn't setting the user default to 0 because it is running the settings page again from didfinishwithoptions. Can anyone advise ?
Any value put into NSUserDefaults is not necessarily written instantly. If you app, for instance, terminates in some unusual way, there is no guarantee, the data will be written.
You can force the system to write to the NSUserDefaults, using synchronize:
[[NSUserDefaults standardUserDefaults] synchronize]
From the documentation:
Writes any modifications to the persistent domains to disk and updates all unmodified persistent domains to what is on disk.
Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.
There is a minor performance penalty doing so.
In iOS6 the following code works fine. After the first launch, the hasLoaded BOOL is YES.
BOOL hasLoaded = [[NSUserDefaults standardUserDefaults] boolForKey:kUserDefaultFirstLaunch];
if (!hasLoaded){
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kUserDefaultFirstLaunch];
[[NSUserDefaults standardUserDefaults] synchronize];
//do first launch stuff
}
But in iOS7, every time I run the app, hasLoaded is NO, even though I'm clearly setting it to YES and synchronizing. I have seen a couple other posts about this but no solutions. This would be such a HUGE bug that I can't believe it's really an Apple bug. Please tell me I'm wrong. Is it really true that no apps can reliably use NSUserDefaults on iOS7?
I've tested this on both the simulator and the device.
After looking at the com.domain.myapp.plist file in the Preferences folder I saw that the kUserDefaultFirstLaunch was actually "2", which is neither true or false. Turns out that I had a typo elsewhere that was using the kUserDefaultFirstLaunch key.
Dumb mistake, but I'm glad it's not a bug in the SDK.
I had the same problem and I think adding the following to the Application Delegate was what fixed it:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults] synchronize];
}
Take your value in string(or any other as per your requirement) and save the string in user defaults
NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
NSString *str=[NSString stringWithFormat:#"%#",[dicUser objectForKey:#"device_token"]];
[userdefault setObject:str forKey:#"YOURKEY"];
Dear Experts and Friends,
Hi, I have been trying to get this down for the whole day, but nothing I have tried seems to work. Basically I would like to save an array of strings (max: 20, nothing heavy weight) using NSDefault. It saves, it loads ok, but not when I killed the App on real device by pressing the Minus Button. I tested it on an iPhone 4gs (iOS 6).
I have tried [[NSUserDefaults standardUserDefaults] synchronize];
in both
applicationWillTerminate:(UIApplication *)application
and
applicationDidEnterBackground:(UIApplication *)application
But it didn't work.
Note: from NSLog, I am getting the values back fine from NSDefault as long as I don't kill the application in background on real device.
Does anyone know why? Many thanks in advance. Please cite with samples.
========================= EDIT (ADDING CODE):
I am adding in some code, in hopes that it will be clearer and that someone can really help me out here.
Thanks in advance.
some action, then
self.count = [NSNumber numberWithInt:[self.count intValue] + 1];
count is a NSNumber
NSData *count = [NSKeyedArchiver archivedDataWithRootObject:self.count];
[[NSUserDefaults standardUserDefaults] setObject:count forKey:#"count"];
[[NSUserDefaults standardUserDefaults] synchronize];
arrayForSavedKey is a NSMutableArray
[[NSUserDefaults standardUserDefaults] synchronize];
NSData *array = [NSKeyedArchiver archivedDataWithRootObject:self.arrayForSavedKey];
[[NSUserDefaults standardUserDefaults] setObject:array forKey:#"arrayForSavedKeys"];
In Both:
applicationWillResignActive:(UIApplication *)application
applicationWillEnterBackground:(UIApplication *)application
I have tried added the following line
[[NSUserDefaults standardUserDefaults] synchronize];
To retrieve the data, I have the Delegate to call a method in class A
by calling:
loadSavedData
and unarchiving like so:
NSData *savedKeys = [[NSUserDefaults standardUserDefaults] objectForKey:#"arrayForSavedKeys"];<br>
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:savedKeys]];<br>
self.arrayForSavedKey = [[NSMutableArray alloc] initWithArray: array];<br>
(There are only a few NSStrings in it)
In which I NSLOG the output and everything is fine SO LONG AS I do not kill the app by the Minus sign.
(But with a real device I cannot see anything from NSLog, of course, because once I killed the App, App will stop running as well in xCode)
I have spent all day today and I am running the end of my wits. Please help.
The applicationWillTerminate: is only called for a few very special cases so don't bother using that.
Using applicationWillEnterForeground: to save your defaults is way too late.
The proper place to call [[NSUserDefaults standardUserDefaults] synchronize]; is in the applicationDidEnterBackground: method. This way, if you app is killed in the background, the defaults have already been saved.
I am downloading data in background.
So if user press home button and force quit app by double click on the app icon, then I need some data cleaning.
I try to do data cleaning in the delegate method applicationWillTerminate but app did not call this method while crashes.
I can not do data cleaning in applicationDidEnterBackground as my app keeps data downloading in background. I search many hours.. but I am clueless about this.
Can anyone tell me, Is there any way to call method(to clean data) before app crashes.
Thanks!
You can't clean up data if your app crashes or is forcefully quit.
You can instead clean the data the next time it's being opened. Depending on what you're doing, you could use a flag inside NSUserDefaults that will let you know if data needs to be cleaned or not.
For instance, when the download starts, you can set:
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:#"pendingDownload"];
[[NSUserDefaults standardUserDefaults] synchronize];
When download finishes:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"pendingDownload"];
[[NSUserDefaults standardUserDefaults] synchronize];
Inside didFinishLaunchingWithOptions:
if([[[NSUserDefaults standardUserDefaults] objectForKey:#"pendingDownload"] boolValue]){
// clean data
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"pendingDownload"];
[[NSUserDefaults standardUserDefaults] synchronize];
}