何时以及为什么要使用NSUserDefaults的synchronize()方法? [英] When and why should you use NSUserDefaults's synchronize() method?

查看:175
本文介绍了何时以及为什么要使用NSUserDefaults的synchronize()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我看了一下NSUserDefaults的synchronize()方法的苹果文档。请参阅下面的参考:

So I've had a look at the apple documentation on the NSUserDefaults's synchronize() method. See below for reference:

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会处理它?并且在内存中的设置更改后立即离开视图会导致更改丢失吗?

However, what I still don't understand is when should this method be called? For example, 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? And does the leaving of the view immediately after a settings change in memory result in that change being lost?

此外,何时可能无法调用synchronize()导致用户设置没有正确更改?

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

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

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?

推荐答案

似乎如此混淆用户默认值。这样想吧。它与您在整个应用程序中提供全局词典基本相同。如果您向全局字典添加/编辑/删除键/值,则该更改会立即显示在代码中的任何位置。由于这个字典在内存中,如果你的应用程序没有持久保存到文件,那么所有这些字典都会丢失。 NSUserDefaults 每隔一段时间自动将字典保存到文件中。

There seems to be so much confusion about user defaults. Think of it this way. It's essentially the same as you having a global dictionary available throughout your app. If you add/edit/remove a key/value to the global dictionary, that change is immediately visible anywhere in your code. Since this dictionary is in memory, all would be lost when your app terminates if it wasn't persisted to a file. NSUserDefaults automatically persists the dictionary to a file every once in a while.

唯一的原因是 synchronize 方法是这样你的应用可以告诉 NSUserDefaults 将字典现在保存,而不是等待最终的自动保存发生。

The only reason there is a synchronize method is so your app can tell NSUserDefaults to persist the dictionary "now" instead of waiting for the automatic saving that will eventually happen.

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

And the only reason you ever need to do that is because your app might be terminated (or crash) before the next automatic save.

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

In my own apps, the only place I call synchronize is in the applicationDidEnterBackground delegate method. This is to ensure the latest unsaved changes are persisted in case the app is terminated while in the background.

我认为很多混乱来自于在开发过程中调试应用程序。在开发期间,您通过调试器中的停止按钮终止应用程序并不罕见。很多时候,这种情况发生在最近的 NSUserDefaults 更改被保留之前。因此,我习惯于在我想确保最新更新被保留的情况下,在调试器中杀死应用程序之前按下Home按钮将我的应用程序置于后台。

I think much of the confusion comes from debugging an app during development. It's not uncommon during development that you kill the app with the "stop" button in the debugger. And many times this happens before the most recent NSUserDefaults changes have been persisted. So I've developed the habit of putting my app in the background by pressing the Home button before killing the app in the debugger whenever I want to make sure the latest updates are persisted.

鉴于上述摘要,让我们回顾一下您的问题:

Given the above summary, let's review your questions:


每次用户更改应用程序的设置时都会调用它吗?

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

不。如上所述,任何更改都会立即自动提供。

No. As described above, any change is automatically available immediately.


或者我应该相信后台api会处理这个?

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

是的,相信自动持久性,但当您的应用进入时调用 synchronize 除外背景。

Yes, trust the automatic persistence with the exception of calling synchronize when your app enters the background.


并且在内存设置更改后立即离开视图会导致更改丢失吗?

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

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

This has no effect. Once you add/edit/delete a key/value in NSUserDefaults, the change is made.


此外,如果调用synchronize()失败,导致用户设置无法正确更改?

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

唯一的时间如果您的应用在最新更改持续之前被终止,则可能会丢失更改。当您的应用进入后台时,调用同步可解决大部分问题。唯一剩下的可能问题是你的应用程序崩溃了。任何尚未保留的未保存更改都将丢失。修复您的应用程序,使其不会崩溃。

The only time a change can be lost is if your app is terminated before the latest changes have been persisted. Calling synchronize when your app enters the background solves most of these issues. The only remaining possible problem is if your app crashes. Any unsaved changes that have not yet been persisted will be lost. Fix your app so it doesn't crash.


此外,调用此方法的成本(性能,内存或其他)是多少?我知道它涉及从磁盘读取和写入磁盘,但这真的需要花费很多功夫吗?

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 来存储大量数据,则速度会慢一些。

The automatic persistence is done in the background and it simply writes a dictionary to a plist file. It's very fast unless you are not following recommendations. It will be slower if you are misusing NSUserDefaults to store large amounts of data.

这篇关于何时以及为什么要使用NSUserDefaults的synchronize()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆