Why is NSUserDefaults not saving my values?

IphoneNsuserdefaults

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

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).

Solution 2 - Iphone

This code works fine for me .

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

if (standardUserDefaults) {
	[standardUserDefaults setObject:myString forKey:@"Prefs"];
	[standardUserDefaults synchronize];
}

Solution 3 - Iphone

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.

Solution 4 - Iphone

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpankajView Question on Stackoverflow
Solution 1 - IphoneNick ForgeView Answer on Stackoverflow
Solution 2 - IphoneKannan PrasadView Answer on Stackoverflow
Solution 3 - IphoneClaus BrochView Answer on Stackoverflow
Solution 4 - Iphonejuan IsazaView Answer on Stackoverflow