jayhu93/Tip-Calculator

No need to "Synchronize" UserDefaults.

regnerjr opened this issue · 2 comments

SettingsViewController attempts to synchronize() user defaults after setting the default tip percentage.

The word on the street, is that you should not really be doing this anymore....

Similarly, when the user changes a setting, you simply call -setObject:forKey: (or one of its convenient type-specific wrappers).

If you find yourself to do anything else to set a preference, again, you probably don't need to. It is almost never necessary to call -synchronize after setting a preference (see Sharing Defaults Between Programs below), and users are generally not capable of changing settings fast enough for any sort of "batching" to be useful for performance. The actual write to disk is asynchronous and coalesced automatically by NSUserDefaults.

The "Sharing Defaults" Section has this to say:

If one process sets a shared default, then notifies another process to read it, then you may be in one of the very few remaining situations that it's useful to call the -synchronize method in: -synchronize acts as a "barrier", in that it provides a guarantee that once it has returned, any other process that reads that default will see the new value rather than the old value. For applications running on iOS 9.3 and later / macOS Sierra and later, -synchronize is not needed (or recommended) even in this situation, since Key-Value Observation of defaults works between processes now, so the reading process can just watch directly for the value to change. As a result of that, applications running on those operating systems should generally never call synchronize.

Full Text: http://dscoder.com/defaults.html
(David Smith, works on the Foundation frameworks)

Also, Still filing issues because I think you're awesome and want to tell you all the stuff that I know about. And hey look relevant we can talk about code and apps and stuff.

Please don't take these as a critique, or anything, (I really do think you're awesome) just me looking to share my knowledge in context.

Thanks @regnerjr for leaving those useful feedbacks! I really appreciate them!
Interesting article too!