最近在做APP卡顿情况原因的排查,发现其中一个容易出现的优化点share preference.edit 经常在主线程中调用commit。通常我们是不需要commit结果的,完全可以替换为apply()方法。至于两者区别可以参考官方说明:
abstract void |
apply()
Commit your preferences changes back from this Editor to the
SharedPreferences object it is editing.
|
abstract SharedPreferences.Editor |
clear()
Mark in the editor to remove
all values from the preferences.
|
abstract boolean |
commit()
Commit your preferences changes back from this Editor to the
SharedPreferences object it is editing.
|