thepoosh
thepoosh

Reputation: 12587

is SharedPreferences access time consuming?

I'm currently trying to test a third party service for my app, and need to identify each test that is being done at every specific run.

Since more than one test can take place every time I run the testApp, I need to Identify every test.

What I thought of, is storing the device name and build (not many devices here), and an index for each test.

private String getTestId(){
    SharedPreferences settings = getPreferences(0);
    SharedPreferences.Editor editor = settings.edit();
    int testNumber = settings.getInt("id", 0);
    editor.putInt("id", testNumber+1);
    editor.commit();
    String id = Build.DEVICE + Build.VERSION.RELEASE+" - test number: "+testNumber;
    return id;
}

Is running this function every time I run a test time consuming, or can I do this without fearing the coast?

if the answer is "time consuming", what would you suggest I do every time I run a test in order to differentiate every test?

Upvotes: 21

Views: 8417

Answers (3)

user1194949
user1194949

Reputation:

I've noticed that when you use methods like putInt() the first time for a specific key it can take a significant amount of time. Besides, it should be equivalent to any other ways of writing to a file.

Upvotes: 1

hooked82
hooked82

Reputation: 6376

The question already has an answer, but in case others come and are looking for a code sample, I put together this utility class for interacting with the SharedPreferences.

Calling commit() will use the apply() method if it's available, otherwise it will default back to commit() on older devices:

public class PreferencesUtil {

    SharedPreferences prefs;
    SharedPreferences.Editor prefsEditor;
    private Context mAppContext;
    private static PreferencesUtil sInstance;

    private boolean mUseApply;

    //Set to private
    private PreferencesUtil(Context context) {
        mAppContext = context.getApplicationContext();
        prefs = PreferenceManager.getDefaultSharedPreferences(mAppContext);
        prefsEditor = prefs.edit();

        //Indicator whether or not the apply() method is available in the current API Version
        mUseApply = Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;
    }

    public static PreferencesUtil getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new PreferencesUtil(context);
        }
        return sInstance;
    }

    public boolean getBoolean(String key, boolean defValue) {
        return prefs.getBoolean(key, defValue);
    }

    public int getInt(String key, int defValue) {
        return prefs.getInt(key, defValue);
    }

    public String getString(String key, String defValue) {
        return prefs.getString(key, defValue);
    }

    public String getString(String key) {
        return prefs.getString(key, "");
    }

    public void putBoolean(String key, boolean value) {
        prefsEditor.putBoolean(key, value);
    }

    public void putInt(String key, int value) {
        prefsEditor.putInt(key, value);
    }

    public void putString(String key, String value) {
        prefsEditor.putString(key, value);
    }

    /**
     * Sincle API Level 9, apply() has been provided for asynchronous operations.
     * If not available, fallback to the synchronous commit()
     */
    public void commit() {
        if (mUseApply)
            //Since API Level 9, apply() is provided for asynchronous operations
            prefsEditor.apply();
        else
            //Fallback to syncrhonous if not available
            prefsEditor.commit();
    }
}

Upvotes: 1

auselen
auselen

Reputation: 28087

About SharedPreferences.

SharedPreferences caches after first load, so disk access to load data will take time but once. You can try to load SharedPreferences early in your test suite to avoid this penalty.

For persisting your data you should opt for SharedPreferences.Editor.apply() instead of SharedPreferences.Editor.commit() since appy is asynchronous. But please do read the documentation about both to see which one applies in your case.

Upvotes: 16

Related Questions