缓存 SharedPreferences 的值有意义吗?
在我当前的 Android 应用程序中,我有几个存储在 SharedPreferences 中的设置以及一个处理对它们的访问的对象。我现在想知道缓存这些值是否有意义,或者访问它们是否并不重要,例如:
public final boolean isxxxEnabled() {
return preferences.getBoolean("xxx", false);
}
而不是
public final boolean isxxxEnabled() {
// check if value changed
// if not, check if value is cached
// decide whether to return cached or new
// cache value
return
}
In my current Android app I have several settings stored in SharedPreferences and one object which handles access to them. I now wonder if it makes sense to cache the values or if doesn't mater much accessing them like:
public final boolean isxxxEnabled() {
return preferences.getBoolean("xxx", false);
}
instead of
public final boolean isxxxEnabled() {
// check if value changed
// if not, check if value is cached
// decide whether to return cached or new
// cache value
return
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
缓存共享首选项并不是真正必要的。您将获得的速度充其量也只是微不足道的,而且它会增加您必须编写的代码。我想说别打扰。
Caching shared preferences isn't really necessary. The speed up you're gonna get will be marginal at best and it's going to increase the code you have to write. I'd say don't bother.