gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 11:09:45 24 4
gpt4 key购买 nike

因此,我查看了有关 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 会处理这个问题吗?在内存中更改设置后立即离开 View 是否会导致更改丢失?

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

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

最佳答案

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

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

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

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

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

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

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?

这没有效果。在 NSUserDefaults 中添加/编辑/删除键/值后,就会进行更改。

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 来存储大量数据,它会变慢。

关于ios - 何时以及为什么应该使用 NSUserDefaults 的 synchronize() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40808072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com