Nsuserdefaults Synchronize Not Saving On

NSUserDefaults not saved after synchronize

You have key and value reversed:

[defaults setObject:userID forKey:kArchivedUsername];

Credit to: Teja Nandamuri

Why is NSUserDefaults not saving my values?

If you terminate your app by pressing the home button (in the Simulator or on the device), your NSUserDefaults will get saved.

If you terminate your app by pressing "Stop" in Xcode (in the Simulator or on the device), your NSUserDefaults might get saved, but there's a good chance they won't. NSUserDefaults persists any changes periodically, and if you terminate the process before they've been persisted, they'll be gone. You can force the save by calling:

[[NSUserDefaults standardUserDefaults] synchronize];


Addendum:

In iOS4 (this answer was originally written when iOS3 was the public release), your NSUserDefaults may not get saved when pressing the home button. Manually calling [[NSUserDefaults standardUserDefaults] synchronize] in applicationDidEnterBackground: should ensure that your NSUserDefaults are saved correctly (this should really be a built-in behaviour IMO).

NSUserDefaults Not Saving Data Objective C

Only thing you are missing is, synchronize you are not telling NSUserDefault to write data on disc.

// *** Get your score value ***
score = [[[NSUserDefaults standardUserDefaults] objectForKey:@"score"] longLongValue];

// *** manipulate it if needed ***
score+=100;

// *** save it back to `NSUserDefaults` ***
[[NSUserDefaults standardUserDefaults] setObject:@(score) forKey:@"score"];

// *** Synchronize your updates to `NSUserDefaults` by calling it, it immediately write data to disc ***
[[NSUserDefaults standardUserDefaults] synchronize];

For more details about NSUserDefaults read Apple Documentation

NSUserDefaults not saving

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.

NSUserDefaults Not Saving Data. Limits?

So after half hour morning work i find the solution and actually the problem. Case such as mine in 90% it is developers fault, so it was.

I had more than 30 user defaults value's, in which for e.g 15 of them had initWithSuitName (Identifier), everything saved with identifier id was working well but saving value without it was broken because of preferance conflicts.

Solution: I replaced unnamed userDefaults with identifier and everything has fixed.

In Case of some problem with NSUserDefaults:

NSUserDefaults - is thread safe.

Synchronize - Saves data immediately

Saving Data - If app will terminate chance of losing data is 50%, after debug Stop button tapped is same scenario. P.S data lose will be 50% if it's not saved in right place.

Debuging NSUserDefaults - in debugger you can simply tipe po [[NSUserDefaults standardUserDefaults] dictionaryRepresentation] and po [[NSUserDefaults standardUserDefaults] valueForKey:@"KeyForSavedData"]

Shared Container Example - if you want to share property for example between app and extension you can simply type appGroup identifier inside initWithSuitName:@"com.Example.ExampleAPP"

NSuserDefaults not saving until i close the app swift

It's hard to post code as a comment so lets try to work here.

Try changing your if statement to this:

if levelfinished() {
defaults.setInteger(nextlevel, forKey: "levelsCompleted")
defaults.synchronize()
levelsCompleted = defaults.intergerForKey("levelsCompleted")
println("level \(levelsCompleted) unlocked)
}

NSUserDefaults not saving integer

Try setting your NSUserDefaults first like this:

NSUserDefaults *userPrefs = [NSUserDefaults standardUserDefaults];

You can also try to use a NSString like this:

NSInteger newValue = [self doSomethingWithOldValue:timestamp];
NSString* newValueString = [NSString stringWithFormat:@"%i", newValue];

[userPrefs setObject:newValueString forKey:aKey];

Also make sure that your aKey is set correctly



Related Topics



Leave a reply



Submit