Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice for NSUserDefaults synchronize

I'm using [[NSUserDefaults standardUserDefaults] synchronize] each time I write anything to the plist. Is that overkill? Or are there adverse effects in doing this?

like image 368
Lauren Quantrell Avatar asked Apr 08 '11 21:04

Lauren Quantrell


People also ask

Why should you avoid synchronization?

Avoid or Minimize Synchronization - Improper synchronization can also cause a deadlock, which can result in complete loss of service because the system usually has to be shut down and restarted. But performance overhead cost is not a sufficient reason to avoid synchronization completely.

What are the best practices for synchronizing methods in Java?

Java Synchronization Best Practices Let's discuss each Synchronization best practices with an example. If only certain operations in the method must be synchronized, use a synchronized block with a mutex instead of synchronizing the entire method. For example:

What should I consider when planning and deploying server-side synchronization?

Consider the following when planning and deploying server-side synchronization. Using one account to process email to all mailboxes is easier to maintain but requires using an account that has access to all mailboxes in Outlook or Exchange. The account must have impersonation rights on Exchange.

Do I need to synchronize all the methods of an object?

Since there is no method that can change the state of any of the object's instance variables once the object is created, there is no need to synchronize on any of the object's methods. This approach works well for objects, which are small and contain simple data types.


2 Answers

Yes it may be overkill but in a simple application will you notice a performance hit? probably not if you are only saving after basic user interaction such as the user selecting their settings. The benefit to calling synchronize more often is if your application may crash and the information you are saving is important, otherwise iOS will save it for you periodically.

like image 138
Joe Avatar answered Oct 03 '22 22:10

Joe


The synchronize method, which is automatically invoked at periodic intervals, keeps the in-memory cache in sync with a user’s defaults database.

Calling it frequently may cause performance issues, but it's not an overkill if it's a small application (like already mentioned) OR if you really need your plist to be up to date with the changes made in the current thread or changes made in some other thread in the application.

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

Probably the only adverse effect you might notice is a negligible decrease in performance.

like image 35
Koushik Ravikumar Avatar answered Oct 03 '22 22:10

Koushik Ravikumar