当(不)滥用NSUserDefaults [英] When (not) to abuse NSUserDefaults

查看:160
本文介绍了当(不)滥用NSUserDefaults的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道什么准则是:

1 - 我可以从NSUserDefaults读取的频率

2 - 我可以合理地存储在NSUserDefaults中的数据量



显然,有多少NSUserDefault可以使用,但是我有困难确定什么是合理的和什么不是。



一些例子:




  • 如果我的游戏有一个选项让电脑成为玩家之一,我会使用NSUserDefaults以保存该布尔值。这很清楚。但是,在我的游戏每次我想知道是否计算机是一个播放器或者我应该使用一个实例变量,而是访问NSUserDefaults也是合理的吗?假设这里我需要检查布尔每秒。答案是相同的是它的100毫秒?


  • 如果我的游戏有50个移动对象,当用户退出应用程序时,我想要存储他们的位置和速度,NSUserDefaults一个合理的地方存储数据? 20个移动对象呢?那么200?



解决方案

不要担心限制。



如果它是首选项,那么它应该在用户默认值。这是用户默认值。



在iOS上,您可能会看到它是偏好设置还是偏好设置,而不是将其(如果可能)放在您的设置包中,以便在设置应用程序中显示和编辑。在Mac OS X上,通常可以通过将它放在首选项窗口中来判断是否是首选项。



当然,这依赖于你的判断。例如,用于Mac的Stanza会出现错误,在其首选项窗口中显示非首选项。



您还可以通过其相反的方式考虑该问题:



此用户创建的数据是什么?



不是用户创建的数据;它是用户覆盖的数据。不但不会失去它,而且它告诉你应该保存它。


I wonder what the guidelines are for:
1 - how often I can read from NSUserDefaults
2 - how much data I can reasonably store in NSUserDefaults

Obviously, there are limits to how much NSUserDefaults can be used but I have trouble determining what's reasonable and what isn't.

Some examples among others:

  • If my game has an option for the computer to be one of the players, I will use NSUserDefaults to save that boolean value. That much is clear. But is it also reasonable to access NSUserDefaults during my game every time I want to know whether the computer is a player or should I be using an instance variable for that instead? Assume here I need to check that boolean every second. Is the answer the same is it's 100 ms instead? What about every 10 s?

  • If my game has 50 moving objects and I want their positions and speeds to be stored when the user quits the app, is NSUserDefaults a reasonable place to store that data? What about 20 moving objects? What about 200?

解决方案

Don't worry about limits. Instead, ask yourself this simple question:

Is this a preference?

If it is a preference, then it should be in user defaults. That's what user defaults is for. If not, then it should be in the Documents directory (or, on the Mac, possibly in Application Support).

On iOS, you might tell whether it's a preference or not by whether it would be appropriate to (if possible) put it in your settings bundle for display and editing in the Settings application. On Mac OS X, you can usually tell whether it's a preference or not by whether it would be appropriate to put it in the Preferences window.

Of course, that relies on your judgment. Stanza for Mac, for example, gets it wrong, putting non-preferences in its Preferences window.

You can also consider the question by its converse:

Is this user-created data?

A preference that you will have a default value for is not user-created data; it is user-overridden data. No less bad to lose it, but it informs where you should keep it.

这篇关于当(不)滥用NSUserDefaults的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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