关于SharedPreferences我们在Android开发中经常使用到,SharedPreferences有apply和commit两种提交方式:
/**
* 保存到本地SharedPreferences的boolean数据
*
* @param context 传入的上下文对象
* @param key 得到数据的标识
* @param value 如果获得不到的默认值
*/
public static void putPreBoolean(Context context, String key, boolean value) {
if (pre==null){
pre = context.getSharedPreferences(CONFIG_NAME, Context.MODE_PRIVATE);
}
pre.edit().putBoolean(key, value).commit();
}
/**
* 保存到本地SharedPreferences的boolean数据
*
* @param context 传入的上下文对象
* @param key 得到数据的标识
* @param value 如果获得不到的默认值
*/
public static void putPreBoolean(Context context, String key, boolean value) {
if (pre==null){
pre = context.getSharedPreferences(CONFIG_NAME, Context.MODE_PRIVATE);
}
pre.edit().putBoolean(key, value).apply();
}
我们查看一下官方给的注释:
/**
* Commit your preferences changes back from this Editor to the
* {@link SharedPreferences} object it is editing. This atomically
* performs the requested modifications, replacing whatever is currently
* in the SharedPreferences.
*
* <p>Note that when two editors are modifying preferences at the same
* time, the last one to call commit wins.
*
* <p>If you don't care about the return value and you're
* using this from your application's main thread, consider
* using {@link #apply} instead.
*
* @return Returns true if the new values were successfully written
* to persistent storage.
*/
boolean commit();
/**
* Commit your preferences changes back from this Editor to the
* {@link SharedPreferences} object it is editing. This atomically
* performs the requested modifications, replacing whatever is currently
* in the SharedPreferences.
*
* <p>Note that when two editors are modifying preferences at the same
* time, the last one to call apply wins.
*
* <p>Unlike {@link #commit}, which writes its preferences out
* to persistent storage synchronously, {@link #apply}
* commits its changes to the in-memory
* {@link SharedPreferences} immediately but starts an
* asynchronous commit to disk and you won't be notified of
* any failures. If another editor on this
* {@link SharedPreferences} does a regular {@link #commit}
* while a {@link #apply} is still outstanding, the
* {@link #commit} will block until all async commits are
* completed as well as the commit itself.
*
* <p>As {@link SharedPreferences} instances are singletons within
* a process, it's safe to replace any instance of {@link #commit} with
* {@link #apply} if you were already ignoring the return value.
*
* <p>You don't need to worry about Android component
* lifecycles and their interaction with <code>apply()</code>
* writing to disk. The framework makes sure in-flight disk
* writes from <code>apply()</code> complete before switching
* states.
*
* <p class='note'>The SharedPreferences.Editor interface
* isn't expected to be implemented directly. However, if you
* previously did implement it and are now getting errors
* about missing <code>apply()</code>, you can simply call
* {@link #commit} from <code>apply()</code>.
*/
void apply();
大概区别如下:
(1)apply是无返回值的,而commit是有返回值,所以使用apply提交之后,无法判定是否提交成功,而commit方式可以返回是否提交成功的布尔值,所以在需要得到返回值的情况下建议使用commit方式
(2)apply操作是一个原子性的操作,是先把数据提交到内存中,然后再通过异步任务提交到硬件磁盘上。而commit是个同步操作,直接把数据提交到硬件磁盘上。因此,在多个并发的提交commit的时候,他们会等待正在处理的commit保存到磁盘后在操作,从而降低了效率。而apply只是原子的提交到内容,后面有调用apply的函数的将会直接覆盖前面的内存数据,这样从一定程度上提高了很多效率。
总结:
在开发中如果我们需要返回值的情况(一般不需要),那就选用commit方式。其他情况,建议选用效率比较高的apply方式。
注意:SharedPreferences能够存储的数据类型:String、Int、Long、Float、Boolean、Set<String>
网友评论