4

I'm working on a GCM based application where the user can subscribe to several topics.

I need to know which topics the users is subscribed to in two places:

  • Main activity - to show Subscribe or Unsubscribe buttons in the UI
  • GCM listener service - to filter messages and handle "obsolete" subscriptions via GcmPubSub. Basically, if the listener gets a message for the topic which is not in the app's list of topics then probably we have an "obsolete" subscription on the GCM server and have to unsubscribe.

So basically I have an activity and a service which both acces some common data and both can modify this data.

I've read that one of the options to share data between an activity and a service is to use shared preferences:

This is suitable for my case as I'd be quite content to share Set<String> which SharedPreferences support. Users will be probably interested in just a few topics (say, max. 10).

Here's my code to check if the user is subscribed to a topic:

SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE);
Set<String> subscribedTopics = preferences.getStringSet(AufzugswaechterPreferences.SUBSCRIBED_TOPICS, Collections.<String>emptySet());
boolean subscribedForTopic = subscribedTopics.contains(topic);

Here's the code to modify the subscription (for instance unsubscribe):

SharedPreferences preferences =
        PreferenceManager.getDefaultSharedPreferences(getContext());
Set<String> topics = new TreeSet<String>(preferences.getStringSet(AufzugswaechterPreferences.SUBSCRIBED_TOPICS, Collections.<String>emptySet()));
topics.remove(topic);
preferences.edit().putStringSet(AufzugswaechterPreferences.SUBSCRIBED_TOPICS, topics).apply();

But now I've got my doubts, if it is an appropriate way. I'll be basically accessing shared preferences for every check (in the UI or on received message) as well as on modifications.

Is this the right way to go? Should I share data between the activity and the service directly via preferences or should I somehow cache values?

Community
  • 1
  • 1
lexicore
  • 42,748
  • 17
  • 132
  • 221
  • 1
    When you say: "Should I share data between the activity and the service directly via preferences or should I somehow cache values" I don't understand why you ask if you should cache somehow. This is what SharedPreferences is already doing for you! Stick with how you have it in my opinion, this seems to be a fairly common pattern, and it works. No need to reinvent the wheel :) – Lucas Crawford Jan 08 '16 at 19:00
  • "Somehow cache" would be, like, copy `Set topics` to activity or the service. Thanks, I'm just an absolute newbie to Android, basically downloaded the Android Studio yesterday so I'm not quite aware of common patterns yet. – lexicore Jan 08 '16 at 19:03
  • Ahh, I see. It is totally not needed, SharedPreferences is fairly efficient and caches the data for you so it is accessible from anywhere that has reference to a Context (either Activity or Application context). So you are okay with just using a utility class to access your data and passing the utility function context to get that data. Welcome to the world of Android, it is by far my favorite development environment and platform to work with – Lucas Crawford Jan 08 '16 at 19:07
  • Note: .apply(); when used on the SharedPreferences does this in a background thread so it also doesn't block the UI thread, just an interesting item I wanted to point out – Lucas Crawford Jan 08 '16 at 19:08
  • Yep, thanks. I've read about `.commit()` vs. `.apply()` so that's intentional. – lexicore Jan 08 '16 at 19:13

1 Answers1

10

There is no need for you to cache SharedPreferences data, as SharedPreferencesImpl already caches the shared data.

cybersam
  • 63,203
  • 6
  • 53
  • 76