NSUserDefaults gets reset

I have an ios application, and lately I have been receiving messages from users about their settings, getting reset. Settings are saved using NSUserDefaults .

My question is, can the default user get corrupt / reset for some reason?

A few notes on how I use it:

  • When I launch my application, I save the default values ​​only if the keys do not exist with registerDefaults (this is done in order to take care of the "first install" scenario).

  • Most of the above reports describe the failure of the application, and then when it opens again, the default settings are reset.

  • My use of NSUserDefaults pretty simple - saving numbers/strings , reading them sometimes, etc. Nothing special. I do not delete the keys.

Some sample code:

For example, I save data in different places using:

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:@"FirstLevel" forKey:@"Level"]; [defaults setInteger:100 forKey:@"Score"]; [defaults synchronize]; 

I read using:

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; int intValue = [defaults integerForKey:@"Score"]; return intValue; 

So, can the default user get reset / corrupt in some way?

More generally, could this be related to the application crash message that was reported even if the crash did not occur directly as a result of writing / reading from user defaults?

+6
source share
2 answers

From what I understand, it is deleted only when the application is uninstalled. Since your NSUserDefaults reset problem was preceded by an application crash, I would start by figuring out the failure problem.

0
source

If you install your application on your device, NSUserDefault will be reinitialized again ... when you test your application on the device, you will connect your device to Xcode, and if you run the program, the values ​​will reset ...

-2
source

Source: https://habr.com/ru/post/958294/


All Articles