何时以及为什么应该使用 NSUserDefaults 的 synchronize() 方法?

When and why should you use NSUserDefaults's synchronize() method?

所以我查看了有关 NSUserDefaults 的 synchronize() 方法的苹果文档。参考如下:

https://developer.apple.com/reference/foundation/userdefaults/1414005-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.

但是,我还是不明白,这个方法应该在什么时候调用?例如,是否应在每次用户更改应用程序设置时调用它?还是我应该相信后台 api 会处理这个问题?在内存中更改设置后立即离开视图是否会导致更改丢失?

另外,什么时候调用 synchronize() 失败会导致用户设置无法正确更改?

此外,调用此方法的成本(性能、内存或其他)是多少?我知道它涉及读写 from/to 磁盘,但在手机上真的需要那么多精力吗?

关于用户默认值似乎有太多的混淆。这样想。它本质上与您在整个应用程序中使用全局词典是一样的。如果您 add/edit/remove 一个 key/value 到全局字典,则该更改会立即在您的代码中的任何位置可见。由于该字典在内存中,如果它没有保存到文件中,则当您的应用程序终止时,所有内容都会丢失。 NSUserDefaults 每隔一段时间自动将字典保存到文件中。

存在 synchronize 方法的唯一原因是您的应用程序可以告诉 NSUserDefaults 保留字典 "now" 而不是等待最终会发生的自动保存。

您需要这样做的唯一原因是您的应用程序可能会在下一次自动保存之前终止(或崩溃)。

在我自己的应用程序中,我唯一调用 synchronize 的地方是在 applicationDidEnterBackground 委托方法中。这是为了确保在应用程序在后台终止时保留最新的未保存更改。

我认为很多困惑来自于在开发过程中调试应用程序。在开发过程中,使用调试器中的 "stop" 按钮终止应用程序的情况并不少见。很多时候这种情况发生在最近的 NSUserDefaults 更改被持久化之前。因此,我养成了在调试器中终止应用程序之前按下主页按钮将我的应用程序置于后台的习惯,只要我想确保最新的更新得以保留。

鉴于以上总结,让我们回顾一下您的问题:

should it be called every time the user changes the app's settings?

没有。如上所述,任何更改都会立即自动可用。

Or should I just trust that the background api is going to handle that?

是的,除了在您的应用程序进入后台时调用 synchronize 之外,相信自动持久化。

And does the leaving of the view immediately after a settings change in memory result in that change being lost?

这没有效果。一旦您 add/edit/delete 在 NSUserDefaults 中输入 key/value,就会发生变化。

Also, when might a failure to call synchronize() result in user settings not getting changed correctly?

更改唯一可能丢失的情况是您的应用程序在最新更改被保留之前终止。当您的应用程序进入后台时调用 synchronize 可以解决大部分问题。唯一剩下的可能问题是您的应用程序是否崩溃。任何尚未保存的未保存更改都将丢失。修复您的应用,使其不会崩溃。

Furthermore, what is the cost (performance, memory or otherwise) of calling this method? I know it involves reading and writing from/to the disk but does that really take that much effort on phones?

自动持久化在后台完成,它只是将字典写入 plist 文件。除非您不遵循建议,否则它非常快。如果您滥用 NSUserDefaults 来存储大量数据,速度会变慢。

据我所知,synchronize 用于立即同步数据,但 iOS 可以以智能方式处理。所以你不需要每次都调用它。如果您每次都调用它,那么它将变成性能问题。

查看苹果文档: Official Link

Apple 的 documentation for synchronize() 已更新,现在显示为:

Waits for any pending asynchronous updates to the defaults database and returns; this method is unnecessary and shouldn't be used.

更新

正如预期的那样,已弃用 Apple Doc

synchronize()

Waits for any pending asynchronous updates to the defaults database and returns; this method is unnecessary and shouldn't be used.

原答案

-synchronize is deprecated and will be marked with the NS_DEPRECATED macro in a future release.

-synchronize blocks the calling thread until all in-progress set operations have completed. This is no longer necessary. Replacements for previous uses of -synchronize depend on what the intent of calling synchronize was. If you synchronized…

— …before reading in order to fetch updated values: remove the synchronize call

— …after writing in order to notify another program to read: the other program can use KVO to observe the default without needing to notify — …before exiting in a non-app (command line tool, agent, or daemon) process: call CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication)

— …for any other reason: remove the synchronize call