经常访问 NSUserDefaults

发布于 2024-11-09 18:37:30 字数 308 浏览 0 评论 0原文

在我的应用程序的逻辑处理过程中,我需要频繁访问用户首选项,并多次访问 10~15 次,以确定需要处理什么以及如何处理。也许这个问题不是关于性能的问题,而是关于正确执行的问题。

目前,每次我需要请求一个值时,我都会执行 [[NSUserDefaults standardUserDefaults] valueForKey:...] 。这是正确的吗?我认为将用户默认值“保存”为 ivar 可以减少额外的工作,但我想知道这是否不会出现同步问题,例如用户更改首选项并且仅在应用程序重新启动时才会更新(因此用户默认对象被重新创建)。

还有更好的办法吗?

During a logic process in my app, I need to access the user preferences frequently, and a bunch of times 10~15 to determine what needs to be processed and how. May this question is not about performance, but about doing it correctly.

Currently I'm doing a [[NSUserDefaults standardUserDefaults] valueForKey:...] each time I need to request a value. Is this correct? I think that "saving" the user defaults as an ivar could reduce extra work, but then I wonder if this won't have sync problems, like if the user changes the preferences and they get updated only if the app is restarted (so the user defaults object is recreated).

Is there any better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一绘本一梦想 2024-11-16 18:37:40

在这个逻辑过程中,用户默认值是否会随着时间的推移而改变?

如果没有,您可以在开始时访问整个过程中需要的每个值,并将结果存储在局部变量中。

然后,您可以根据需要多次使用这些变量,而不必每次都使用用户默认值读取数据。

但是,如果在逻辑过程正在进行时这些值正在更改,那么从默认值访问它们可能是唯一的方法。

就性能而言,访问 10-15 次不会产生任何不利影响。如果您长时间每秒访问 10-15 次,那么您可能会遇到一些响应问题。

Do the values in the user defaults change over time during this logic process?

If not, you could access each value that you'll need throughout the process once at the start and store the results in local variables.

Then you can use those variables as many times as you like without having to hit the user defaults reading the data each time.

However, if those values are being changed while your logic process is ongoing, then accessing them from the defaults is probably the only way.

In terms of performance, accessing it 10-15 times isn't going to have any adverse effect. If you were accessing it 10-15 times per second for a prolonged period of time, then you might encounter some responsiveness issues.

分分钟 2024-11-16 18:37:38

如果将默认对象保存到 ivar 中,就不会有任何问题。请注意,它是一个单例,它的指针不会改变。

You won't have any problem if you save the defaults object to an ivar. Notice it's a singleton and its pointer won't change.

扛刀软妹 2024-11-16 18:37:36

别担心,它非常快,我不相信有更好的方法,这就是类的使用方式。

NSUserDefaults 类在内部缓存这些值,因此查找速度非常快。 [NSUserDefaults standardUserDefaults]实例变量 的开销非常小,如果您在代码中执行 500 万次,您甚至不会注意到它。

优化此问题的唯一正确方法是改进您的逻辑,使用指针而不是 NSUserDefaults 基本上是字典等来缓存您自己使用的值。

Don't worry about it, it's extremely fast and I do not believe there is a better way, it's the way the class is meant to be used.

The NSUserDefaults class caches the values internally so the lookup is extremely fast. The overhead of [NSUserDefaults standardUserDefaults] vs an instance variable is so small that you wouldn't even notice it if you did it 5 million times in your code.

The only proper way of optimising this would be by improving your logic, caching the values you're using yourself with a pointer rather than the dictionary that NSUserDefaults basically is etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文