Do Not Call NSUserDefaults synchronize for iOS 8 及以上的权威来源在哪里?
Where Is Authoritative Source of Do Not Call NSUserDefaults synchronize for iOS 8 and Above?
我记得在 iOS 8 及更高版本上不应该费心调用 NSUserDefaults 的同步方法。我还从一个 non-Apple source and another 中读到了相同的提示。但是,我无法从 Apple 找到权威来源。这是几年前的 WWDC 视频吗? Apple 的权威来源是什么?
读这个:NSUserDefaults
并浏览了其中的一些内容:WWDC 2014 Videos
嗯,看完你的评论(请将 link 添加到你的 post)我得出了这个结论:
文章指出您不应该调用-[NSUserDefaults synchronize]
,因为这是性能缺陷。
我只在我真的需要写入默认值时才使用它,例如当需要它在不同的 class 中更新时。我从来没有觉得需要。
总结一下;出于性能原因,最好不要调用它。如果您需要立即更新数据,请调用它。
我看到的最准确的来源是以下来自 David Smith 的推文和博客 post,他是 Apple 的 NSUserDefaults 开发人员:https://twitter.com/catfish_man/status/674727133017587712 and http://dscoder.com/defaults.html
Reminder that the only thing -[NSUserDefaults synchronize] does is wait. That’s not useless, but the situations it’s useful in are uncommon.
博客post阐述:
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.
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.
我记得在 iOS 8 及更高版本上不应该费心调用 NSUserDefaults 的同步方法。我还从一个 non-Apple source and another 中读到了相同的提示。但是,我无法从 Apple 找到权威来源。这是几年前的 WWDC 视频吗? Apple 的权威来源是什么?
读这个:NSUserDefaults 并浏览了其中的一些内容:WWDC 2014 Videos
嗯,看完你的评论(请将 link 添加到你的 post)我得出了这个结论:
文章指出您不应该调用-[NSUserDefaults synchronize]
,因为这是性能缺陷。
我只在我真的需要写入默认值时才使用它,例如当需要它在不同的 class 中更新时。我从来没有觉得需要。
总结一下;出于性能原因,最好不要调用它。如果您需要立即更新数据,请调用它。
我看到的最准确的来源是以下来自 David Smith 的推文和博客 post,他是 Apple 的 NSUserDefaults 开发人员:https://twitter.com/catfish_man/status/674727133017587712 and http://dscoder.com/defaults.html
Reminder that the only thing -[NSUserDefaults synchronize] does is wait. That’s not useless, but the situations it’s useful in are uncommon.
博客post阐述:
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.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.