经常访问NSUserDefaults [英] Accessing NSUserDefaults Often

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

问题描述

在我的应用程序的逻辑过程中,我需要频繁访问用户首选项,并且一堆次10〜15以确定需要处理的内容以及如何处理。可能这个问题不是关于性能,而是关于正确的做。

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.

目前我正在做一个 [[NSUserDefaults standardUserDefaults] valueForKey:.. 。] 每次我需要请求一个值。它是否正确?我认为保存用户默认为一个ivar可以减少额外的工作,但我不知道这是否不会有同步问题,如果用户更改首选项,它们只有在应用程序重新启动时更新(因此

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).

推荐答案

不要担心,它非常快,我不相信有一个更好的方式,这是类的意思使用的方式。

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.

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

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.

唯一正确的优化方法是改进逻辑,缓存你使用的值,而不是NSUserDefaults基本上是等的字典。

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.

这篇关于经常访问NSUserDefaults的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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