SharedPreferences commit跟apply的区别
来源:互联网 发布:电脑编程代码 编辑:程序博客网 时间:2024/05/17 22:05
- commit方法
- apply方法
- 总结
大多数人在二十岁或三十岁就死了,他们变成了自己的影子,往后的生命只是不断的一天天复制自己。——罗曼·罗兰《约翰•克里斯多夫》
今天被人问到了几个基础问题,平时没有去注意,弄了个”尬回”,借此机会总结下。
本篇回答SharedPreferences的commit跟apply方法的区别。说实话平时用到的时候只是习惯性的选中apply方法,其中具体的区别没有深究,是时候看看源码一探究竟了。
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 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方法有下面的特点
- 存储的过程是原子操作
- commit方法有返回值,设置成功为ture,否则为false
- 同时对一个SharedPreferences设置值最后一次的设置会直接覆盖前次值
- 如果不关心设置成功与否,并且是在主线程设置值,建议用apply方法
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 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();
apply特点如下
- 存储的过程也是原子操作
- apply没有返回值,存储是否成功无从知道。
- apply写入过程分两步,第一步先同步写入内存,第二部在异步写入物理磁盘。并且写入的过程会阻塞同一个SharedPreferences对象的其他写入操作。
原子操作的意思是”不可中断的一个或一系列操作”,通俗的讲一个操作一旦开始,在结束前不会被打断。比如常见的++i操作就不具有原子性,因为它实际上包含了3个步骤,1-读取i的值,2-对读取的i值加1,3-写入加1后的值。单独看此三个步骤都是原子操作,但组合起来就是非原子操作了。
总结
commit相对与apply效率较低,commit直接是向物理介质写入内容,而apply是先同步将内容提交到内存,然后在异步的向物理介质写入内容。这样做显然提高了效率。
还有几个问题后续分篇总结。
1. Activity界面显示完全会回调哪个方法
2. ActivityA跳转ActivityB,个自的生命周期回调情况是什么
3. wakelock的种类
阅读全文
0 0
- SharedPreferences commit跟apply的区别
- android SharedPreferences apply和commit的区别
- SharedPreferences中commit和apply的区别
- SharedPreferences的commit和apply
- SharedPreferences.Editor 的commit方法与apply方法的区别
- Android中SharedPreferences的apply()与commit()的区别
- SharedPreferences.Editor的apply和commit方法区别
- 关于SharedPreferences apply() 和commit() 区别
- SharedPreferences 的commit和apply分析
- SharedPreferences.Editor commit ->apply
- 从源码角度分析sharedPreferences的commit()与apply()的区别
- commit和apply的区别
- SharedPreferences.editor():apply()commit()区别
- Android数据存储之SharedPreference的commit()跟apply()的区别
- SharedPreferences数据的两种保存方法: apply、commit
- sharedpreferences的两种提交方式apply commit
- SharePreference的commit与apply的区别
- SharedPrefrence 的 apply() 和 commit() 区别
- Retrofit2与RxJava的使用
- ASP.NET 使用LinkButton做登录按钮时,回车无法提交解决方案
- Git和SVN的区别
- 新特性总结-JDK1.7
- 面试复习----多线程编程
- SharedPreferences commit跟apply的区别
- Ueditor富文本编辑器替换去除自动追加p标签
- 递归实现快速排序
- Ubuntu配置TFTP
- Js 怎么遍历json对象所有key及根据动态key获取值
- JS实现无限层级的树状的算法
- python read file(f,csv)
- 深入理解css BFC 模型
- 自定义布局