简体   繁体   English

过度使用 NSUserDefaults

[英]Using NSUserDefaults too much

I'm using NSUserDefaults very extensively.我非常广泛地使用 NSUserDefaults。 I'm saving inside NSUserDefaults many arrays of information about user, and using them later at another view.我在 NSUserDefaults 中保存了许多 arrays 的用户信息,稍后在另一个视图中使用它们。

My question is, is this a wrong approach?我的问题是,这是一种错误的方法吗? or this can be a problem at sending to AppStore?或者这可能是发送到 AppStore 的问题?

Thank you..谢谢..

There's no hard and fast rule about what or how much information can be stored in NSUserDefaults (except that you should never store secure data).关于在 NSUserDefaults 中可以存储什么或多少信息没有硬性规定(除了你永远不应该存储安全数据)。 You certainly won't get rejected from the App Store unless you abuse NSUserDefaults to the point of causing crashes.你肯定不会被 App Store 拒绝,除非你滥用 NSUserDefaults 到导致崩溃的地步。

I'm confused by your statement about using in another view, though.不过,我对您关于在另一个视图中使用的陈述感到困惑。 NSUserDefaults is intended for persisting small amounts of data across sessions, not to transfer data between objects in an application. NSUserDefaults 旨在跨会话保留少量数据,而不是在应用程序中的对象之间传输数据。 If you are using it for the latter you should revisit your application architecture.如果您将它用于后者,您应该重新审视您的应用程序架构。

Yes it is a wrong approach, but I do not believe that can be a problem to you when you send it to the AppStore.是的,这是一种错误的方法,但我认为当您将它发送到 AppStore 时,这不会成为您的问题。

What you probably have to do is add some configuration files and you manage that, or maybe use core data in your project, to give you a more precise answer only knowing your project.您可能需要做的是添加一些配置文件并对其进行管理,或者在您的项目中使用核心数据,以便仅了解您的项目即可为您提供更准确的答案。

Best regards.最好的祝福。

EDIT to add better explanation when use NSUserDefaults编辑以在使用 NSUserDefaults 时添加更好的解释

You only wants to use the NSUserDefaults to store configurations, that will be default to the user, like the user favorite color for your background for example.您只想使用 NSUserDefaults 来存储配置,这将是用户的默认设置,例如用户最喜欢的背景颜色。

You do not want to store in the NSUserDefaults, passwords, temporary information, if you are doing a game, you will not store there your ranking, and things like that.你不想在 NSUserDefaults 中存储密码、临时信息,如果你正在玩游戏,你不会在那里存储你的排名,以及类似的东西。

From apple doc来自苹果文档

The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. NSUserDefaults class 提供了一个用于与默认系统交互的编程接口。 The defaults system allows an application to customize its behavior to match a user's preferences.默认系统允许应用程序自定义其行为以匹配用户的偏好。 For example, you can allow users to determine what units of measurement your application displays or how often documents are automatically saved.例如,您可以允许用户确定您的应用程序显示的测量单位或自动保存文档的频率。 Applications record such preferences by assigning values to a set of parameters in a user's defaults database.应用程序通过将值分配给用户默认数据库中的一组参数来记录此类偏好。 The parameters are referred to as defaults since they're commonly used to determine an application's default state at startup or the way it acts by default.这些参数称为默认值,因为它们通常用于确定应用程序在启动时的默认值 state 或默认情况下的行为方式。

It is not preferred to save too much data in userdefaults.最好不要在 userdefaults 中保存太多数据。 Istead you can use plist or Core Data to store your data( this is a good Core Data Tutorial ).相反,您可以使用 plist 或核心数据来存储您的数据( 这是一个很好的核心数据教程)。 And also NSUSerDefaults is difficult to change&remove objects from, so try to use it just to store username&password ect as much as you can.而且 NSUSerDefaults 很难更改和删除对象,所以尽量使用它来存储用户名和密码等。

You wont get reject from the App Store, but its not really good practice to do it.你不会被 App Store 拒绝,但这样做并不是很好的做法 Consider using "Core-data" for storing results and values.考虑使用“核心数据”来存储结果和值。 And little NSNotifications and NSUserDefaults for communicating between viewControllers.以及用于 viewController 之间通信的少量NSNotificationsNSUserDefaults

If you are using NSUserDefaults mainly for communication between different VC's, I would suggest using Delegates ( Message Passing (or) CallBacks )如果您主要使用NSUserDefaults进行不同 VC 之间的通信,我建议使用Delegates (消息传递(或)回调)

Here's a useful thread .这是一个有用的线程 Essentially, go to your project folder and look at the pList file size.基本上,go 到您的项目文件夹并查看 pList 文件大小。

The responses I found helpful were to go to your project file and look at the plist file size:我发现有用的回复是 go 到您的项目文件并查看 plist 文件大小:

5 objects is almost nothing. 5个对象几乎没有。 You'll be fine!你会没事的!


On my machine, I have about 28 megs of data in my user defaults.在我的机器上,我的用户默认值中有大约 28 兆数据。 That's not causing any problems at all.这根本不会造成任何问题。


From general programming experience with arrays I would guess performance starts to rapidly decay when you get into 1000's, depending on element size.根据 arrays 的一般编程经验,我猜当你进入 1000 时性能开始迅速下降,具体取决于元素大小。 Therefore in a program I wouldn't have an issue storing a couple of hundred elements.因此,在一个程序中,我不会有存储几百个元素的问题。 This said I would probably start using a sqlite3 database or coredata, sooner rather than later if I were you.这表示我可能会开始使用 sqlite3 数据库或 coredata,如果我是你的话,宜早不宜迟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM