NSUserDefaults not saving

2019-02-22 02:03发布

问题:

I am having a problem in my sprite kit app where my NSUserDefaults variable is not working. In createSceneContents (which I know is being called)

if (![defaults objectForKey:@"obj"]) {
    difficultyLabel.text = @"Difficulty: Easy";
    [defaults setObject:@"Easy" forKey:@"obj"];
} else {
    difficultyLabel.text = [NSString stringWithFormat:@"Difficulty: %@", [defaults objectForKey:@"diff"]];
}

and then when you click on the SKLabelNode to change the difficulty and this code is being called

if ([label.text isEqualToString:@"Difficulty: Easy"]) {
            label.text = @"Difficulty: Hard";
            [defaults setObject:@"Hard" forKey:@"obj"];
            NSLog(@"%@",[defaults objectForKey:@"obj"]);

        } else {
            label.text = @"Difficulty: Easy";
            [defaults setObject:@"Easy" forKey:@"obj"];
            NSLog(@"%@",[defaults objectForKey:@"obj"]);

but when I stop the program and run it again, it always just says Difficulty: Easy. Any suggestions?

回答1:

As rmaddy's answer explains, NSUserDefaults won't immediately write data to long-term storage.

The values are saved in temporary memory when you call setObject:forKey:, so if you ever try to call objectForKey: after setObject:forKey:, it will return the value in memory if it exists, and if not, it goes to look for what's been saved to long-term storage.

In the background, the device will eventually save these values to permanent storage. It's something the OS handles, but in normal operation of your app, it shouldn't be necessary to immediately store these values to permanent storage most of the time, so let the OS do this at times when its been optimized to do so (I imagine the OS probably synchs every app's user defaults at once on some regular schedule, and as much as possible, tries to do this when the processor is idle probably).

But with that said, as rmaddy explains, if you've got something that crucially needs to be saved to the permanent storage immediately, you can always call synchronize. Keep in mind though... as long as your app isn't killed while in debug mode, the values you've set to be stored in user defaults will be stored.

But for your own sake, if you want to be absolutely certain, you can put it a call to synchronize in applicationDidEnterBackground as rmaddy suggests. Keep in mind though, this method isn't called if you kill the app.



回答2:

When you say you "stop the program", what do you mean? Are you running in the debugger and killing the app and rerunning it again? NSUserDefaults doesn't persist your changes immediately. Either call [defaults synchronize] after writing important data or at least add such a call to your app delegate's applicationDidEnterBackground method. And then be sure you put your app in the background before killing it to ensure the data is written first.



回答3:

To store:

NSString * str = @"name123";
NSUserDefaults *def=[NSUserDefaults standardUserDefaults];
[def setObject:str forKey:@"Key"];
[def synchronize];
NSLog(@"str = = %@",str);

To Retrieve:

NSUserDefaults *def=[NSUserDefaults standardUserDefaults];
NSString *str2 = [def valueForKey:@"Key"];
NSLog(@" Saved str  = = %@",str2);


回答4:

I found another reason why NSUserDefaults not saving,following code can make value in NSUserDefaults not saving:

NSUserDefaults *def= [[NSUserDefaults standardUserDefaults] init];
...
NSUserDefaults *def= [[NSUserDefaults standardUserDefaults] initWithSuiteName:@"123"];

You can search NSUserDefaults in your project to catch those init twice bug.