NSUserDefaults not stored properly

Hi guys I am having problems with NSUserDefaults and I do not quite understand what is happening

My application has 5 levels, and each level does the same with NSUserDefaults (extracts the default levels, changes the value when the user plays the level, and then sets the default values ​​and synchronizes at the end of the level) the first 4 levels ... work without a hitch but the last level does not save the value. The application does not crash, and the last level is not the last thing that happens, and I even have the default synchronization when the application terminates. Is there a maximum size in NSUserDefaults or is there anything that might think that I don’t have, I will post the code below, but as I said, the first four levels work fine

//header NSUserDefaults *userData; @property(nonatomic,retain) NSUserDefaults *userData; //class file //Sets the boolean variables for the class to use userData = [NSUserDefaults standardUserDefaults]; boolOne = [userData boolForKey:@"LevelFiveBoolOne"]; boolTwo = [userData boolForKey:@"LevelFiveBoolTwo"]; boolThree = [userData boolForKey:@"LevelFiveBoolThree"]; boolFour = [userData boolForKey:@"LevelFiveBoolFour"]; boolFive = [userData boolForKey:@"LevelFiveBoolFive"]; boolSix = [userData boolForKey:@"LevelFiveBoolSix"]; boolSeven = [userData boolForKey:@"LevelFiveBoolSeven"]; //End Of Level [userData setBool:boolOne forKey:@"LevelFiveBoolOne"]; [userData setBool:boolTwo forKey:@"LevelFiveBoolTwo"]; [userData setBool:boolThree forKey:@"LevelFiveBoolThree"]; [userData setBool:boolFour forKey:@"LevelFiveBoolFour"]; [userData setBool:boolFive forKey:@"LevelFiveBoolFive"]; [userData setBool:boolSix forKey:@"LevelFiveBoolSix"]; [userData setBool:boolSeven forKey:@"LevelFiveBoolSeven"]; [userData synchronize]; 

When, when switching to the view using these default values, they are correct, but when I exit the application and restart it, these values ​​are not saved, each other level performs the same process, this is the only level that does not work.

I stared at this for quite some time, and I hope that someone there has encountered the same problem and can give me some idea of ​​how they solved it.

Thank you at Advance BWC

+6
iphone nsuserdefaults
source share
4 answers

NSUserDefaults may not be able to save depending on how the process ends.

There is additional information in this answer: Why does NSUserDefaults not save my values?

+13
source share

Just in case, if someone can handle this: when the user saves NSDictionary or NSArray (or mutable objects of both of them) by default and they have an NSURL object, it will not save data during synchronization!

+4
source share

Somewhere you have something like:

 // load the default values for the user defaults userDefaultsValuesPath=[[NSBundle mainBundle] pathForResource:@"UserDefaults" ofType:@"plist"]; userDefaultsValuesDict=[NSDictionary dictionaryWithContentsOfFile:userDefaultsValuesPath]; // set them in the standard user defaults [[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsValuesDict]; 

If the initial default settings that you configure do not have default values ​​for LevelFive, then the calls will fail.

Check the return value from -synchronize for errors.

+2
source share

I decided to leave this problem aside and continue development, which included adding things after the fifth level, so that the user can go through the levels and return to the main menu, etc. etc .... and I'm not sure why, but userDefaults are now saved for the fifth level, so I don’t know if this was because before the fifth level was the last thing the application did, and even despite that it didn’t stop itself and didn’t do other things, maybe it wasn’t written to the disk by default ... I’m still not sure what happened, but now it works, and I can’t make it not to see, can i get a sync error ...

thanks for the help

0
source share

All Articles