我在工作中用到了editor.apply();提交数据,在Android 中存储数据时经常用SharedPreference,今天看了sharedPreference.Editor 的提交方式特意收集和总结了一下apply() 和commit方法的异同
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.
|
从api可以看出:
1. apply没有返回值而commit返回boolean表明修改是否提交成功
2. apply是将修改数据原子提交到内存, 而后异步真正提交到硬件磁盘, 而commit是同步的提交到硬件磁盘,因此,在多个并发的提交commit的时候,他们会等待正在处理的commit保存到磁盘后在操作,从而降低了效率。而apply只是原子的提交到内存,后面有调用apply的函数的将会直接覆盖前面的内存数据,这样从一定程度上提高了很多效率。
由于在一个进程中,sharedPreference是单实例,一般不会出现并发冲突,如果对提交的结果不关心的话,建议使用apply,当然需要确保提交成功且有后续操作的话,还是需要用commit的。