7

THE SCENARIO

I have a class that makes use of a request list set by the user. The request list is stored in SharedPreferences. The dilemma I'm facing is to whether to keep an instance of the request list or to read from SharedPreferences every time the request list is needed (which is very frequent).

Also not that Gson is used to deserialize the object.

The code goes like this:

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;
}

THE GOAL

My concern is that if I keep around an instance of the request list, and there are multiple instances of this class, an update to the request list in one instance of the class would not be reflected in the rest of the instances until they are recreated.

Thus, I'm leaning towards reading from SharedPreferences unless there is a better way to keep the request list objected updated in all instances.

THE QUESTION

(1) So, how efficient is it to read the same key from SharedPreferences quite frequently by multiple instances of the object? and (2) Is there a better way to keep the request list objected updated in all instances?

fahmy
  • 3,543
  • 31
  • 47
  • 1
    if you don't need to persist the request data, you can just create a singleton class or use a static variable to store it. – Surely May 24 '15 at 02:35
  • 1
    How are you displaying your list items? If you're using an adapter, you can just make your list static or a singleton like the previous commenter suggests, then do a notifyDataSetChanged() to update your views. – hungryghost May 24 '15 at 02:39
  • @xwhyLikeThis Yes, I was thinking along the lines of singleton. But hesitant dive right in to it due the controversy around the singleton design pattern. http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – fahmy May 24 '15 at 04:37
  • @fahmy ...I just know there is such a discussion about singleton. Personally I think singleton is quite easy to use and every java developer knows such a pattern and it can solve the problem and it has better performance compared to sharedPreference, so why not? Anyway, you can also just use a static variable. – Surely May 24 '15 at 04:56

1 Answers1

5

So there are a couple of approaches you can take to this.

First, your object is small - re-reading SharedPreferences thousands of times would hardly be noticeable. It's not like SharedPreferences is on a remote drive or has a "bad connection."

Second, if you don't like that answer, then you need a DAO (Data Access Object). SharedPreferences is a form of this already. It provides a means to store and retrieve data with confidence that you have the most recent data available. But, if you feel like you can improve on it's optimization (because it's generic, and this is your app), then you can provide access to you data through a static object that performs both "read" and "write" operations. This will guarantee that access to the object is done with the most recent data. Of course, you will need to be thread aware, etc. (something that is not always guaranteed by SharedPreferences).

Next, you could persist your data in a database and use Cursors or other built-in or custom DAOs. This requires another level of complexity and a lot of overhead, but is useful when several components of your app might need access to the data, provide updates or needs real-time monitoring of changes because background threads or other objects may make modifications that will change your app behavior or result in UI updates.

Last, you could use more complex data stores like a Content Provider. This is really required for cases where you want/need other apps to access data provided by your app (and your app may also consume the data). That's a complex solution and implementation is well outside the scope of this question.

But I mention it because you seem interested in being certain that frequent reads of SharedPreferences is acceptable. It definitely is acceptable - otherwise there would be something else besides it, databases and Content Providers.

Jim
  • 10,172
  • 1
  • 27
  • 36
  • Thanks. So it seems that re-reading `SharedPreferences` is more acceptable than making the class a singleton I would assume. – fahmy May 24 '15 at 04:43