I've been searching for a while, do I need to do synchronize if I am doing:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kType];
After that, should I do?:
[[NSUserDefaults standardUserDefaults] synchronize];
Waiting for you answers in this matter.
The user's defaults database is stored on disk as a property list or plist. A property list or plist is an XML file. At runtime, the UserDefaults class keeps the contents of the property list in memory to improve performance. Changes are made synchronously within the process of your application.
The purpose of [default synchronize]; is to make the user defaults get written on disk immediately. You don't need to call it explicitly, iOS already does it at appropriate moments. So you can remove that line. In fact, it's a performance problem if you call synchronize every time you set a default.
To remove a key-value pair from the user's defaults database, you need to invoke removeObject(forKey:) on the UserDefaults instance. Let's update the previous example. The output in the console confirms that the removeObject(forKey:) method works as advertised.
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.
Quoting this post in stackoverflow, the answer from DarkDust:
The purpose of
[default synchronize];
is to make the user defaults get written on disk immediately. You don't need to call it explicitly, iOS already does it at appropriate moments. So you can remove that line. In fact, it's a performance problem if you callsynchronize
every time you set a default.Prior to iOS 7, the user defaults where always synchronized when the application transitioned into background. As of iOS 7 that is no longer the case so you might want to call
synchronize
in your app delegate'sapplicationDidEnterBackground:
or register to theUIApplicationDidEnterBackgroundNotification
notification to do that.From the documentation for
-[NSUserDefaults synchronize]
: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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With