Android performance: cost of SharedPreferences

和自甴很熟 提交于 2019-12-12 15:09:01

问题


When my app starts, I populate a container class with values from my shard prefs. The idea was to handle the SharedPreferences and PreferenceManager once since I'm guessing they're heavy.
Here's a sample:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cont); 
StorageClass.lifespan = Integer.parseInt( prefs.getString("lifespan", "8") );
StorageClass.hiRate = Integer.parseInt( prefs.getString("hiRate", "71") );
//and on and on for all preferences 

Other activities then request these values.
But as my app grows, main memory is becoming tight.

Would it be better to have every requester make an instance of SharedPreferences and get the value they want?

Thanks


回答1:


The same SharedPreferences object is returned each time you request the preferences of the same name. If that wasn't the case, there would need to be a lot of complicated code inside the framework to deal with multiple SharedPreference objects keeping their state in sync when it is changed by one of them.

(This is kind-of the idea behind calling it "shared" preferences.)



来源:https://stackoverflow.com/questions/6682105/android-performance-cost-of-sharedpreferences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!