2012-09-24 17 views
17

Attualmente sto provando a testare un servizio di terze parti per la mia app e ho bisogno di identificare ogni test che viene eseguito in ogni esecuzione specifica.l'accesso a SharedPreferences richiede molto tempo?

Poiché più di un test può essere eseguito ogni volta che eseguo il testApp, è necessario identificare ogni test.

Quello che ho pensato, è memorizzare il nome del dispositivo e costruire (non molti dispositivi qui), e un indice per ogni 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; 
} 

è in esecuzione questa funzione ogni volta che ho eseguito un tempo di prova che consumano, o posso farlo senza temere la costa?

se la risposta è "che richiede tempo", cosa suggeriresti di fare ogni volta che eseguo un test per differenziare ogni test?

+2

Definire "tempo" - Accesso 'SharedPreferences' = accesso il filesystem che richiede più tempo rispetto alle semplici operazioni di memoria. – zapl

+0

c'è un modo migliore di firmare un test, o è questo il modo migliore di firmare senza influenzare il test? – thepoosh

risposta

12

Informazioni su SharedPreferences.

SharedPreferences cache dopo il primo caricamento, quindi l'accesso al disco per caricare i dati richiederà tempo ma una volta. Puoi provare a caricare SharedPreferences all'inizio della tua suite di test per evitare questa penalità.

Per la persistenza dei dati è necessario optare per SharedPreferences.Editor.apply() anziché SharedPreferences.Editor.commit() poiché appy è asincrono. Ma per favore leggi la documentazione su entrambi per vedere quale si applica al tuo caso.

1

Ho notato che quando si utilizzano metodi come putInt() la prima volta per una chiave specifica, può richiedere molto tempo. Inoltre, dovrebbe essere equivalente a qualsiasi altro modo di scrivere su un file.

1

La domanda ha già una risposta, ma nel caso in cui altri vengano e stiano cercando un esempio di codice, ho messo insieme questa classe di utilità per l'interazione con SharedPreferences.

Calling commit() utilizzerà il metodo apply() se è disponibile, altrimenti questo verrà impostato di nuovo a commit() sui vecchi dispositivi:

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(); 
    } 
} 
Problemi correlati