0

I used to SharedPreferences.apply() method. When this method is called very often, then it hangs the application. Commit() method is very slow, but is working properly.

You can get the ANR in my example. Fold and unfold the activity!

public class Main extends Activity {

@Override
public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.main);

    new Thread(new Runnable() {
        @Override
        public void run() {

            while(true) {
                SharedPreferences.Editor ed = getEditor();
                ed.putString(getUUID(), getUUID());
                ed.apply();


                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}



public static String getUUID() {
    return UUID.randomUUID().toString();
}


final private String BASE = "BASE";
private SharedPreferences shadPrefBase = null;
SharedPreferences getSharedPreferences() {
    if(shadPrefBase == null) {
        shadPrefBase = getSharedPreferences(BASE, Context.MODE_MULTI_PROCESS);
    }
    return shadPrefBase;
}


private SharedPreferences.Editor editorShared = null;
private SharedPreferences.Editor getEditor() {
    if(editorShared == null) {
        editorShared = getSharedPreferences().edit();
    }
    return editorShared;
}
}

Fold and unfold the activity!

M D
  • 47,665
  • 9
  • 93
  • 114
Evgeniy
  • 25
  • 1
  • 6
  • 2
    Fold and unfold the activity! – Evgeniy Mar 20 '15 at 12:44
  • Don`t you think in multi threaded environment, you will face this issue of hand as you are getting different instance of editor , but internally it will work on same copy of shared preferences. This might get issue related to issue related to synchronization. – Roll no1 Mar 20 '15 at 12:59
  • causing a new editor, the problem is not solved – Evgeniy Mar 20 '15 at 13:02

1 Answers1

5

Every 10 milliseconds, indefinitely, you are forking a background thread via the apply() call, all of which are going to queue up as they attempt to do I/O on the same data. That is not going to give you good results.

Beyond that, I would be very careful about sharing Editor instances across threads the way you are.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Can you explain in more detail? – Evgeniy Mar 20 '15 at 13:05
  • @Evgeniy: Not really. `apply()` forks a thread. You are creating 100 threads a second on a mobile device. That is not going to work well, regardless of what those threads do, and it will be worse when they are all contending for the same resource. Moreover, you do not need `apply()` in this case, as you are doing your work on a background thread already. `apply()` is for when you are saving preferences from the main application thread (e.g., in response to a UI event). – CommonsWare Mar 20 '15 at 13:14
  • @Evgeniy: Stop trying to persist `SharedPreferences` 100 times a second. – CommonsWare Mar 20 '15 at 13:17
  • I have to keep the 3000 objects and more. Commit takes a very long time. In my example, just a pause in 10 milliseconds. But in the actual situation, is evaluated, and then saving. If there is a break, then I lose the data, so you need to be saved after each action. – Evgeniy Mar 20 '15 at 13:21
  • @Evgeniy: "I have to keep the 3000 objects and more" -- `SharedPreferences` is not designed for that. Use SQLite. "If there is a break, then I lose the data" -- then batch your data in memory and write it to disk less frequently, such as once per second, and do so in a separate thread from the thread that is generating your data. – CommonsWare Mar 20 '15 at 13:23
  • I can not get all the objects at once and save them. I get the objects gradually recursively down the tree of objects. All objects have an arbitrary number of fields, they are stored as a string. I desperately need to be able to quickly pull on an object ID. Key / value database for that. Maybe you know like mongodb database for android? Such a framework would be ideal. – Evgeniy Mar 20 '15 at 13:53