Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUserDefaults not being saved in simulator?

My [NSUserDefaults standardUserDefaults] are not being saved. Everytime I close the app and start it again, it reverts back. I don't have my iOS device handy so I am not sure if this will happen on device, but none of the other apps are doing this in simulator which leads me to believe there's something wrong with my code. I don't know what part of code I should include here, it's just simple add/modify keys in NSUserDefaults and as I said it runs fine during the app, but not after i restart it...

I know I can call synchronize but Apple advices against it and says I should only call it if it's a must... so.

What could be going wrong?

like image 904
user635064 Avatar asked May 17 '11 15:05

user635064


2 Answers

Your process is possibly terminated improperly so that NSUserDefaults do not have a chance to be stored. See also this and mostly this.

The suggestion in the second post I link to is to call synchronize in applicationDidEnterBackground:

Keep also in mind that terminating your app by stopping it in Xcode most often does not save user defaults.

like image 191
sergio Avatar answered Oct 31 '22 13:10

sergio


Are you restarting from Xcode / debugger? Try sending the app to background with the Home button first. I think the framework synchronizes automatically then.

like image 40
Steven Kramer Avatar answered Oct 31 '22 11:10

Steven Kramer