Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it mandatory to call NSUserDefaults synchronize method?

Please, anyone, help me: Is calling NSUserDefaults's synchronize() method mandatory?. If I don't call it, what will happen? My application is working fine without it.

like image 454
Balagurubaran Avatar asked May 27 '16 13:05

Balagurubaran


People also ask

What is UserDefaults standard synchronize ()?

NSUserDefaults automatically persists the dictionary to a file every once in a while. The only reason there is a synchronize method is so your app can tell NSUserDefaults to persist the dictionary "now" instead of waiting for the automatic saving that will eventually happen.

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 .

What is NSUserDefaults in IOS?

NSUserDefaults caches the information to avoid having to open the user's defaults database each time you need a default value. When you set a default value, it's changed synchronously within your process, and asynchronously to persistent storage and other processes. Important.

What is use of UserDefaults?

Storing Default Objects The UserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Boolean values, and URLs. These methods are described in Setting Default Values.


2 Answers

No.

Since iOS12, it isn't mandatory anymore. Apple says:

This method is unnecessary and shouldn't be used.

You can find more information on iOS12 release note:

UserDefaults

NSUserDefaults has several bug fixes and improvements:

Removed synchronization requirements. It's no longer necessary to use synchronize, CFPreferencesAppSynchronize, or CFPreferencesSynchronize. These methods will be deprecated in a future version of the OS.

Now that you don't need to call these synchronization methods, the performance characteristics of NSUserDefaults and Preferences Utilities are slightly different: The time taken for enqueueing write operations is now paid by the writing thread, rather than by the next thread to call synchronize or do a read operation.

like image 110
CoyBit Avatar answered Nov 13 '22 15:11

CoyBit


From the docs:

Because 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) or if you want to update the user defaults to what is on disk even though you have not made any changes.

Meaning that if you kill the app right after something is written to the defaults without the periodic interval catching it, it will get lost. You probably did not kill the app right after a write event yet which is why your app seems to work fine so far.

like image 36
luk2302 Avatar answered Nov 13 '22 15:11

luk2302