Android – reads and retains instances of objects from SharedPreferences
scene
I have a class that uses the request list set by the user. The request list is stored in SharedPreferences. The dilemma I face is whether to keep the instance of the request list or read it from SharedPreferences every time I need the request list (this is very frequent)
Nor does it mean that gson is used to deserialize objects
The code is as follows:
public List<PrayerTimesCalculator.Time> getDefaultRequestList() {
if (mRequestList != null) return mRequestList;
// Try getting request list from preferences;
Gson gson = new Gson();
String json = mSharedPref.getString(KEY_PREF_REQUEST_LIST, null);
Type listType = new TypeToken<List<Time>>() {
}.getType();
mRequestList = gson.fromJson(json, listType);
if (mRequestList != null) return mRequestList;
// Create default list;
mRequestList = Arrays.asList(
Time.DAWN,
Time.MORNING,
Time.AFTERNOON,
Time.EVENING,
Time.MID_NIGHT);
return mRequestList;
}
target
My concern is that if I keep instances of the request list and the class has multiple instances, the update of the request list in one instance of the class will not be reflected until other instances are recreated
Therefore, I prefer to read from shared preferences unless there is a better way to keep the request list updated in all instances
problem
(1) So, how efficient is it that multiple instances of an object often read the same key from SharedPreferences? (2) Is there a better way to keep the request list updated in all cases?
resolvent:
Therefore, you can take several methods
First of all, your object is small - Rereading SharedPreferences thousands of times is hardly noticeable. It's not like SharedPreferences on a remote drive or "bad connection"
Secondly, if you don't like the answer, you need a Dao (data access object). SharedPreferences is already in this form. It provides a way to store and retrieve data, and you can safely obtain the latest data. However, if you think you can improve its optimization (because it is general, this is your application), you can execute "read" and The static object of "write" operation provides access to data. This will ensure that the access to the object is completed with the latest data. Of course, you need to be thread aware, etc. (shared preferences does not always guarantee this)
Next, you can save the data in the database and use cursors or other built-in or custom Daos. This requires another level of complexity and a lot of overhead, but it is very useful when multiple components of the application may need to access data, provide updates, or monitor changes in real time, Because the background thread or other objects may modify your application behavior or UI update results
Finally, you can use more complex data storage, such as content provider. If you want / need other applications to access the data provided by your application (and your application may also use the data), you really need to do so. This is a complex solution, and the implementation is completely beyond the scope of this problem
But I mention it because you seem interested in ensuring that frequent reading of SharedPreferences is acceptable. It's absolutely acceptable - among other things, databases and content providers