Strange iPhone SDK NSUserDefaults behaviour - iphone

I'm experiencing some strange NSUserDefaults behaviour. I save my data, then synchronize, then do a NSlog of the NSUserDefaults to make sure its saved. The data is shown as saved properly from the NSlog, but when I completely close the app (double click home button and kill the app) when it restarts the value has not changed. Whats even stranger is if I repeat the code several times by saving NSUserDefaults, killing the app, reloading the NSUserDefaults sometimes it gets saved properly and other times it does not.
I read this post Strange NSUserDefaults behavior but I am calling synchronize so I don't think thats my problem.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:#"MyString" forKey:#"testString"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%#", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
Any help is greatly appreciated.
What really confuses me is if the NSLog of standardUserDefaults shows the data is saved why wouldn't it be after the app has been killed???
Thanks
UPDATE
Looks like the issue was with a mixup in mutable and immutable arrays i was saving to NSUserDefaults. I used mutablecopy when I was retrieving the data and seemed to solve the issue.

For getting the value you could use something like this
NSUserDEfaults *ud = [NSUserDefault standarUserDefaults];
[ud objectForKey=#"YourKey"];
If it returns nil it hasn't been saved.

Related

NSUserDefault doesn't work on real device(iPhone)

In my App, I am trying to implement SettingView by NSUserDefault. So, I wrote program code as follows:
Method example of saving user setting:
+ (void)setUserAutoPlaySetting:(BOOL)setting {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:setting forKey:kAutoPlaySettingKey];
[userDefaults synchronize];
}
This method is received user setting as BOOL and saves by NSUserDefault.
I confirmed my App save user setting on simulator but not on real Device.
In addition, this method is enable to save NO but disable to save YES on a real device.
Can anyone please tell how can I overcome this problem? Thanks.

Consistent NSUserDefaults saving

So in my app, I was saving some integer keys in NSUserDefaults,like around a minimum of 20.
Something like this but many:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:1 forKey:#"game1"];
[prefs setInteger:1 forKey:#"game2"];
[prefs setInteger:1 forKey:#"game3"];
So I was just wondering, should I call:
[[NSUserDefaults standardUserDefaults] synchronize];
Everytime I save?
Because sometimes I'm experiencing an inconsistency of the integer I'm saving and have been loading either with stopping, running the app in xCode or from the Home Button.
I have read a couple of articles on a site that stated:
In iOS4, your User Defaults may not get saved when pressing the home button. Manually calling [[NSUserDefaults standardUserDefaults] synchronize] in applicationDidEnterBackground: should ensure that your User Defaults is saved correctly.
Is this true and the same in iOS5? Or are there more accurate ways, shorter ways of saving integers. Thanks for the help.
Apple's documentation says this:
"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."
So, depending on the urgency of the data you are settings, you may of may not need to call synchronize. Full documentation is here.
No you don't need to, when you call setInteger:forKey: on prefs, it automatically gets saved.

NSUserDefaults is not always saving my NSString data

I'm running into something a bit puzzling. I have some basic string data that I'm trying to save to the NSUserDefaults, like so:
NSString *state = #"finished";
[[NSUserDefaults standardUserDefaults] setObject:state forKey:#"downloadFinished"];
[[NSUserDefaults standardUserDefaults] synchronize];
When the app starts, one of the ways it makes decisions on which view to load is to check the value of this user setting:
NSString *savedState = [[NSUserDefaults standardUserDefaults] stringForKey:#"downloadFinished"]
if(savedState && [savedState length]) {
// Show view
}
Every once in a while, I've noticed that this value fails to load, even though I know for sure that this value was saved previously (or at least, the app tried to save it previously). When this happens, I'm getting a nil value back when I lookup the "downloadFinished" key from the user defaults.
Has anyone run into this before? Is there any knowledge out there on why this might happen? How likely is it that the save to the user's defaults cache and defaults database fails, and is there anyway to further prevent this from happening?
I'm going to try the solution mentioned in Why is NSUserDefaults not saving my values?, which forces another synchronize when the app enters the background, but I'm wondering if anyone has deeper knowledge on the matter.
I am guessing some data type issue here.
Can you try this instead, and see if you get something?
NSString *savedState = [[NSUserDefaults standardUserDefaults] objectForKey:#"downloadFinished"]
Notice objectForKey usage

Why won't my NSUserDefaults load?

So I have an iphone app that presents a modal view when it starts if the user has not registered. Once they register, it saves a user ID into NSUserDefaults. When the app starts at a later time, it needs to load this user id in the appdelegate file.
//Registration.m
NSUserDefaults *savedUID = [NSUserDefaults standardUserDefaults];
[savedUID setObject:UID forKey:#"user_id"];
//AuctionTracAppDelegate.m
NSUserDefaults *savedID = [NSUserDefaults standardUserDefaults];
NSString *uidString = [savedID stringForKey:#"user_id"];
My problem is that when I try to load this value at a separate execution, the object I saved is always null. I know for a fact that it is saving the string object correctly. Also, this weird behavior JUST surfaced. This exact code was working earlier, then after working on a totally unrelated file, this code fails. I'm clueless as to what happened. Any hints?
I believe that you need to call the synchronize method.

Why is NSUserDefaults not saving my values?

Hi I am trying to use NSUserDefaults to save some default values in database. I am able to save the values in the NSUserDefaults (even checked it in NSLog).
Now I need the values in app delegate when the application is restarted. But I am not getting anything in the NSUserDefaults. Following is my code from my class where I save the values in NSUserDefaults:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:appDel.dictProfile forKey:#"dict"];
NSLog(#"%#",[prefs valueForKey:#"dict"]);
Following is my code from App Delegagte:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSLog(#"%#",[prefs valueForKey:#"dict"]);
the above code always returns me null.
Can some one please help me?
If you terminate your app by pressing the home button (in the Simulator or on the device), your NSUserDefaults will get saved.
If you terminate your app by pressing "Stop" in Xcode (in the Simulator or on the device), your NSUserDefaults might get saved, but there's a good chance they won't. NSUserDefaults persists any changes periodically, and if you terminate the process before they've been persisted, they'll be gone. You can force the save by calling:
[[NSUserDefaults standardUserDefaults] synchronize];
Addendum:
In iOS4 (this answer was originally written when iOS3 was the public release), your NSUserDefaults may not get saved when pressing the home button. Manually calling [[NSUserDefaults standardUserDefaults] synchronize] in applicationDidEnterBackground: should ensure that your NSUserDefaults are saved correctly (this should really be a built-in behaviour IMO).
This code works fine for me .
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:myString forKey:#"Prefs"];
[standardUserDefaults synchronize];
}
You didn't say whether you are running on a device or in the simulator, but if you restart the application in the simulator, all preferences will be reset between launches if you launch from Xcode. The preferences will only be preserved if you relaunch from the simulator itself.
In my case I was saving and retrieving a string. When I synchronized after saving and then retrived in another thread, it was not working properly. The problem was solved by synchronizing both after saving and before retreiving.