Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUserDefaults not saving properly

I have issues with NSUserDefaults and I don't quite understand what is 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.

like image 747
BDubCook Avatar asked Sep 13 '09 21:09

BDubCook


People also ask

How much data can you store in NSUserDefaults?

It appears the limit is the maximum file size for iOS (logically), which is currently 4GB: https://discussions.apple.com/thread/1763096?tstart=0. The precise size of the data is circumscribed by the compiler types (NSData, NSString, etc.) or the files in your asset bundle.

Is NSUserDefaults encrypted?

NSUserDefaults is easy to incorporate into your application and unfortunately, that means it is frequently misused by developers of all skill levels. Because NSUserDefaults stores all data in an unencrypted .


2 Answers

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

This answer has more info: Why is NSUserDefaults not saving my values?

like image 75
Nick Forge Avatar answered Sep 30 '22 18:09

Nick Forge


Just in case someone runs accross this: When storing an NSDictionary or NSArray (or mutable Objects of both of them) in the user defaults and they have an NSURL Object stored, it won't save the data on synchonize!

like image 45
NicTesla Avatar answered Sep 30 '22 18:09

NicTesla