SharedPreferences中的commit和apply方法
来源:互联网 发布:java包装一年工作经验 编辑:程序博客网 时间:2024/06/11 00:00
1. SharedPreferences对象获取
context类中的getSharedPreferences
方法可以获取一个SharedPreferences对象
private File makeFilename(File base, String name) { if (name.indexOf(File.separatorChar) < 0) { return new File(base, name); } throw new IllegalArgumentException( "File " + name + " contains a path separator"); }
其本质也是在文件不存在的情况下new一个File
2. 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方法是有一个boolean的返回值
- 当数据变化进行存储时是一个原子性的操作
- 当两个editor对象同时对一个共享的preferences参数进行操作时,永远都是最后一个调用commit方法的editor变更了最后的数据值
3. 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方法是没有返回值的
- 当两个editor同时对preferences对象编辑时,也是最后一个调用apply方法的对象编辑数据
- apply的提交操作也是原子性的
4. commit和apply方法的区别:
commit和apply虽然都是原子性操作,但是原子的操作不同,commit是原子提交到数据库,所以从提交数据到存在Disk中都是同步过程,中间不可打断。
而apply方法的原子操作是原子提交的内存中,而非数据库,所以在提交到内存中时不可打断,之后再异步提交数据到数据库中,因此也不会有相应的返回值。
所有commit提交是同步过程,效率会比apply异步提交的速度慢,但是apply没有返回值,永远无法知道存储是否失败。
在不关心提交结果是否成功的情况下,优先考虑apply方法。
作者:doMyselfzy
链接:http://www.jianshu.com/p/c8d10357c939
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
阅读全文
0 0
- SharedPreferences中的commit和apply方法
- SharedPreferences中的commit和apply方法
- SharedPreferences的commit和apply
- SharedPreferencesUtils 工具类 及 SharedPreferences apply和commit方法异同
- SharedPreferences.Editor的apply和commit方法区别
- android SharedPreferences apply和commit的区别
- SharedPreferences 的commit和apply分析
- 关于SharedPreferences apply() 和commit() 区别
- SharedPreferences中commit和apply的区别
- SharedPreferences.Editor commit ->apply
- SharedPreferences.Editor 的commit方法与apply方法的区别
- SharedPreferences数据的两种保存方法: apply、commit
- SharedPreference的commit()和apply()方法
- android SharedPreferences apply() 与 commit()
- SharedPreferences commit跟apply的区别
- SharedPreference.Editor的apply()方法和commit()方法的区别
- Andrfoid SharedPreferences 偏好设置 保存list集合对象 commit和apply区别
- SharedPreference.Editor的apply和commit方法异同
- okhttp3的详细介绍
- Android 进阶之路:常见设计模式之代理模式一
- 《数字技术》连载29:第4章 信息的寄存传输和转换 第2节 信息的译码,译码器
- 在线与本地myeclipse安装svn
- 项目管理利器—maven(二)
- SharedPreferences中的commit和apply方法
- Oracle性能调整之--DML语句性能调整
- 前端小白--工具篇(一)雪碧图CssSprite
- datatables表格分页和导出的例子
- 如何快速转载CSDN中的博客
- Mac使用阿里云Code提供的Git仓库做版本控制
- ehcache的简单运用
- ListenableFuture
- Android Handler机制的简单理解