How to remove some key/value pair from SharedPreferences?

前端 未结 6 1017
滥情空心
滥情空心 2020-12-12 23:26

How to remove some key/value pair from SharedPreferences ? I have put and I to remove that from prefs.

6条回答
  •  情歌与酒
    2020-12-13 00:07

    It is important to note that, unless you're planning on doing something with the return value of the commit() call, there is absolutely no reason for using the synchronous commit() call instead of the asynchronous apply() call.

    Keep in mind that if you're calling this from the main/UI thread, the UI is blocked until the commit() has completed. This can take upwards of around 100ms as apposed to about 5ms for the apply. That may not seem like much, but if done continually throughout an application, it will certainly add up.

    So, unless you're planning on doing something like this, hopefully on a separate thread:

    editor.remove(String key); 
    boolean success = editor.commit();
    if (!success) { 
        // do something 
    }
    

    You should instead be doing this:

    editor.remove(String key); 
    editor.apply();
    

提交回复
热议问题