views:

1606

answers:

3

Hey Guys I'm having issues with NSUserDefaults and I don't quite understand what's going on

My App has 5 levels and each level does the exact same thing with NSUserDefaults (Retrieves the levels defaults, changes the value as the user plays the level and then sets the defaults and syncronizes at the end of the level) the first 4 levels...work without a hitch but the last level doesn't save the values. The app doesn't crash and the last level isn't the very last thing that happens, And I even have the defaults synchronized when the application terminates. Is there a max size on the NSUserDefaults or is there anything anyone can think of that I haven't, I'll post the code below but like I said the first four levels work perfectly

//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 I switch to the view that uses these defaults they values are correct but when I terminate the application and restart it, these values aren't saved, every other level does the exact same process this is the only level that doesn't work.

I've stared at this for quite awhile and I'm hoping someone out there has run into the same problem and can give me some insight on how they resolved it.

Thank you in Advance BWC

A: 

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 defaults you are setting up do not have LevelFive defaults, then the calls would fail.

Check the return value from -synchronize for errors.

mahboudz
A: 

I decided to put this issue aside and continue development which included adding things after level five so the user can loop through levels and return to the main menu and so on and so forth...and I'm not sure why but the userDefaults are saving for level five now so I don't know if it's because before level five was the very last thing the application did and even though it didn't terminate itself and did other things maybe it wasn't actually writing the defaults to disk...I'm still not sure what was wrong but it's working now and I can't get it to fail to see if I can get an error with the synchronize...

Thanks for the help

BWC
+2  A: 

NSUserDefaults might not have a chance to save depending on how the process is terminated.

This answer has more info: http://stackoverflow.com/questions/2622754/nsuserdefault-not-able-to-save-values/2622940#2622940

Nick Forge