Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is SharedPreferences access time consuming?

Tags:

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?

like image 404
thepoosh Avatar asked Sep 24 '12 14:09

thepoosh


People also ask

Are SharedPreferences persistent?

Shared preferences is an Android class that allows apps to store key-value pairs of primitive data types. Once saved, information in shared preferences will persist across sessions.

How can I access SharedPreferences from another activity?

You can create a new shared preference file or access an existing one by calling one of these methods: getSharedPreferences() — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from any Context in your app.

Can SharedPreferences be hacked?

It's not a secret that SharedPreferences is not a secure place to store sensitive data because the data is saved in simple key-value pairs in an XML file. In some cases, it can easily be hijacked.

How primitive data can keep persistent using SharedPreferences?

Shared preferences is one of the few methods used in Android development to save and retrieve primitive data types even when an Android application is terminated and relaunched. It saves data with a specific key that is later used to retrieve the data initially stored with it.


2 Answers

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.

like image 171
auselen Avatar answered Sep 21 '22 16:09

auselen


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();
    }
}
like image 44
hooked82 Avatar answered Sep 20 '22 16:09

hooked82