Question

Consider the following example:

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def synchronize];

if([def objectForKey:@"Test_Value_1"] != nil){
  [def removeObjectForKey:@"Test_Value_1"];
  [def setBool:YES forKey:@"Test_Value_2"];
}

if (![def boolForKey:@"Test_Value_2"]){
...
}

Is is possible that the Test_Value_1 gets removed but the Test_Value_2 is not set? I'm not synchronizing after i change these values. My understanding is that the changes will kept in memory and then synchronized at some time later. So i should be save. The only possible way would be if after [def removeObjectForKey:@"Test_Value_1"]; the app would crash but i think thats highly unlikely.

Was it helpful?

Solution

"The sncronize method writes any modifications to the persistent domains to disk and updates all unmodified persistent domains to what is on disk."

"This method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit)"

and in your case the app will not crash as you first check weather there is an object for the key "Test_Value_1" in user defaults, so if the key is removed then it will not execute the code in if condition.

OTHER TIPS

From Apple Docs:

At runtime, you use an NSUserDefaults object to read the defaults that your application uses from a user’s defaults database. NSUserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value. The synchronize method, which is automatically invoked at periodic intervals, keeps the in-memory cache in sync with a user’s defaults database.

So if your application crashes in between the time interval of storing the value in cache and before they get store in DB, your application may not have updated value.

synchronize is used to write user defaults immediately to disk.

before ios7 iOS already does it at appropriate moments, but in ios7 its not the case so you have to explicitly synchronize user defaults.

For further details please check Apple doc

and enter link description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top