Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Reading / Writing Preferences An Expensive Operation?

I have a preference that controls whether or not my app plays a sound whenever the user clicks a button (which is done quite often, think of a calculator). Each time the user clicks the button the following method is called:

private void playButtonClickSound() {
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(parentActivity);
    boolean sounds = sharedPrefs.getBoolean("prefSounds", false);
    if (sounds) {
        // blah
    }
}

I was thinking that reading preferences might be an expensive operation (similar to an I/O operation because preferences are persisted) and that since the user clicks buttons so often it might be a bad idea to do it this way.

In general is it a bad idea to read/write preferences frequently? If so, might there be another way such as registering a preference change listener to get notified when a preference changes?

like image 409
Jan Tacci Avatar asked Mar 19 '13 05:03

Jan Tacci


People also ask

How do we get access to the preference?

To get access to the preferences, we have three APIs to choose from: getPreferences() : used from within your Activity, to access activity-specific preferences. getSharedPreferences() : used from within your Activity (or other application Context), to access application-level preferences.

What is SharedPreferences?

A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared. This page shows you how to use the SharedPreferences APIs to store and retrieve simple values.

Is SharedPreferences secure Android?

Shared preferences are not secure as we can simply view the data stored within the shared preferences and can easily access data within that file. To make the data stored in shared preferences secure we use encrypted share preferences which are more secure and the data stored in them is encrypted.

What is the maximum number of shared preferences you can create?

there is no limit in Shared Preference. Save this answer.


1 Answers

Frankly I do all of mine on the UI thread, whether or not I should, and I've never noticed a slight amount of hesitation even on slower devices. It's pretty damn quick. That said, it is I/O, so doing it asynchronously certainly wouldn't be a bad thing. For writes, if you're targeting API 9 and above, you can use apply() instead of commit() which does it asynchronously for you.

As for your question on a preference change listener, yes you can do that as well:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
    @Override
    public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
        if("my_preference_key".equals(key) {
            //Handle it here
        }
    }
}
like image 175
Kevin Coppock Avatar answered Oct 02 '22 14:10

Kevin Coppock