Data in NSUserDefaults is deleted every time the application is restarted
I see this strange behavior that seems to be specific to my particular device or my building environment (my colleague doesn't have this issue when generating the same code). Every time I launch the app from Xcode, the data
disappears. The same thing happens when I kill the application and restart it.
I tried to restart Xcode, my laptop, device, clean project and received data - nothing helped. I am calling
after data has been updated, so it should persist. But this is not the case. And I don't see any errors in the console.
What is the cause of this problem? What should I do to fix this? Is this caused by some hidden project setting?
I would also like to know if the data is reset on restart or not saved when called
. Can I check the contents of the file in which they are saved
UPDATE: Interesting detail. After killing the application in the Simulator, I cannot start it from Xcode again (getting "
The operation couldn’t be completed. (LaunchServicesError error 0.)
"). I have to use
iOS Simulator -> Reset Content and Settings
to start the application again in the emulator.
UPDATE 2: I tried to find the file the iPhone Simulator stores
in as suggested here , but it's not there.
source to share
There are no settings that can be disabled in NSUserDefaults, however there are a few considerations to keep in mind:
- Before adding and extracting them from NSUserDefaults, make sure you are archiving and deleting objects other than simple values.
- Make sure you understand the amount of data being saved (App, Device, ...), look at the corresponding methods in the NSUserDefault class.
- Make sure your third party libraries don't reset NSUserDefault, that was the problem in my case. Debug at application startup and try to figure out if you have a value at the beginning and when you will "lose it". This helped in my case.
source to share