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

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

问题描述

所以我查看了关于 NSUserDefaults 的同步()方法的苹果文档.请参阅下文以供参考:

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-同步

当前页面为:

由于此方法会定期自动调用,因此仅当您无法等待自动同步时(例如,如果您的应用程序即将退出)或者您想更新用户默认设置为启用的内容时才使用此方法即使您没有进行任何更改,磁盘也是如此.

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?

此外,何时调用同步()失败会导致用户设置未正确更改?

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 更改被持久化之前.因此,我养成了这样的习惯:每当我想确保保留最新更新时,在调试器中杀死应用程序之前,先按主页按钮将我的应用程序置于后台.

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.

此外,何时调用同步()失败会导致用户设置未正确更改?

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

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

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 的同步()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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