15

I am trying to use SharedPreferences to store some user settings for my app. I have this code in my Activity.onCreate method:

sharedPreferences = context.getSharedPreferences("MMPreferences", 0);
soundOn = sharedPreferences.getBoolean("soundOn", true);

but it gives me this error (it is the getBoolean that generates the error):

11-10 16:32:24.652: D/StrictMode(706): StrictMode policy violation; ~duration=229 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=2079 violation=2

and the result is that the value is not read and I also get the same error when I try to write to the SharedPreferences with this code (it is the commit that generates the error):

SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("soundOn", soundOn);
editor.commit();

The only answers for this error I can find is about a strict mode warning, but my code actually fails to read/write the SharedPreferences key/value data.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Neigaard
  • 3,726
  • 9
  • 49
  • 85
  • Possible duplicate of [Should accessing SharedPreferences be done off the UI Thread?](http://stackoverflow.com/questions/4371273/should-accessing-sharedpreferences-be-done-off-the-ui-thread) – rds Nov 05 '15 at 20:27

2 Answers2

22

You must do fileSystem operations on a separate thread, then the error will go away.

you can also turn off the StrictMode (but i am not recommending that)

StrictMode.ThreadPolicy old = StrictMode.getThreadPolicy();
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(old)
    .permitDiskWrites()
    .build());
doCorrectStuffThatWritesToDisk();
StrictMode.setThreadPolicy(old);
Frank
  • 16,476
  • 7
  • 38
  • 51
  • Ok I now create a thread on Activity.onCreate and this thread reads properties in startup and checks for dirty properties and writes those if needed, and I stop the thread on Activity.onPause and start it again on Activity.onResume. Sounds about right? – Neigaard Nov 10 '12 at 16:23
  • 2
    It works for sure, and I have tested that the thread is not alive when the app is in the background so it looks good. Just wanted to hear if you wizards thought it sounded like a good way to do it ;) – Neigaard Nov 10 '12 at 16:36
  • @Frank what "doCorrectStuffThatWritesToDisk() " mean ? Thanks – Vinay Feb 10 '16 at 05:59
  • It is your "business logic" that writes things to the disk. Basicly i am turning off strict mode, write, turn strict mode back on. – Frank Feb 10 '16 at 07:42
5

If you have reactive (RxJava) configured in your Android project, you can take advantage of its properties like schedule a task on an I/O-specific Scheduler, for instance:

public void saveFavorites(List<String> favorites) {
    Schedulers.io().createWorker().schedule(() -> {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        Gson gson = new Gson();
        String jsonFavorites = gson.toJson(favorites);
        editor.putString(Constants.FAVORITE, jsonFavorites);
        editor.apply();
    });
}
yaircarreno
  • 4,002
  • 1
  • 34
  • 32