当前位置: 首页 > news >正文

android SharedPreferences apply和commit的区别

1.apply没有返回值而commit返回boolean表明修改是否提交成功2.apply是将修改数据原子提交到内存, 而后异步真正提交到硬件磁盘, 而commit是同步的提交到硬件磁盘3.apply方法不会提示任何失败的提示

apply的效率高一些,如果没有必要确认是否提交成功建议使用apply。


The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

 
 

 

 
 

调用 getSharedPreferences()获取对应的的文件,该函数实现功能如下:

 
 
  1. //Context类静态数据集合,以键值对保存了所有读取该xml文件后所形成的数据集合  
  2. private static final HashMap<File, SharedPreferencesImpl> sSharedPrefs =   
  3.        new HashMap<File, SharedPreferencesImpl>();   

可以看到他有一个Map, 而针对SharedPreferencesImpl里面,由会有map, 这样也就可以证明, 为什么SharedPreference被广泛使用了。 他在普通时刻,内容是从内存里面直接读取的, 只有在第一次启动时,是IO操作。

 

2.  apply() 和 commit()的区别

/**  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 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. 如果你不考虑返回值,你在主线程中已经使用commit,那么你可以考虑替换使用apply
*
* @return Returns true if the new values were successfully written
* to persistent storage.
*/

apply方法的注释:

* <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.

apply不同意commit,commit是同步的去更改硬盘上的东西,而apply是先直接更改内存中的, 然后异步的去更改应硬盘中的内容。

不用去担心线程安全问题, 因为如果一个其他的线程去commit,而刚好有一个还没有完成的apply,commit会被阻塞到异步线程提交完成。
*
* <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. 如果你真的可以忽略返回值,恰好SharedPreferences又是单例模式的,那就可以安全的用apply来替换commit
*
* <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.  也不需要去关心android组件的声明周期。 框架会保证完成所有apply之后,才切换状态。

 

转载于:https://www.cnblogs.com/Ph-one/p/4810316.html

相关文章:

  • S5P4418 uboot 分析
  • 液晶常用接口“LVDS、TTL、RSDS、TMDS”技术原理介绍
  • Android 开发之 ---- 底层驱动开发(一)
  • AOSP
  • api
  • Linux内核OOM机制的详细分析
  • 友好博客集
  • android系统平台显示驱动开发简要:LCD基本原理篇『一』
  • android系统平台显示驱动开发简要:LCD常用接口篇『二』
  • cadence allegro PCB中怎么使查找元件时屏幕不移动
  • FrameBuffer
  • android系统平台显示驱动开发简要:Samsung LCD接口篇『三』
  • android系统平台显示驱动开发简要:LCD驱动调试篇『四』
  • 驱动之路-platform简例按键驱动☆☆☆
  • Android帧缓冲区(Frame Buffer)硬件抽象层(HAL)模块Gralloc的实现原理分析[转]
  • Angular 2 DI - IoC DI - 1
  • C++11: atomic 头文件
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • Essential Studio for ASP.NET Web Forms 2017 v2,新增自定义树形网格工具栏
  • JavaScript的使用你知道几种?(上)
  • Java知识点总结(JavaIO-打印流)
  • Logstash 参考指南(目录)
  • MySQL数据库运维之数据恢复
  • node入门
  • NSTimer学习笔记
  • Three.js 再探 - 写一个跳一跳极简版游戏
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 分布式事物理论与实践
  • 解析 Webpack中import、require、按需加载的执行过程
  • 可能是历史上最全的CC0版权可以免费商用的图片网站
  • 在Mac OS X上安装 Ruby运行环境
  • [Shell 脚本] 备份网站文件至OSS服务(纯shell脚本无sdk) ...
  • 我们雇佣了一只大猴子...
  • #AngularJS#$sce.trustAsResourceUrl
  • (52)只出现一次的数字III
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (附源码)ssm基于web技术的医务志愿者管理系统 毕业设计 100910
  • (免费领源码)Python#MySQL图书馆管理系统071718-计算机毕业设计项目选题推荐
  • (三)centos7案例实战—vmware虚拟机硬盘挂载与卸载
  • (四)模仿学习-完成后台管理页面查询
  • (万字长文)Spring的核心知识尽揽其中
  • (转)Scala的“=”符号简介
  • ****Linux下Mysql的安装和配置
  • **登录+JWT+异常处理+拦截器+ThreadLocal-开发思想与代码实现**
  • .NET CF命令行调试器MDbg入门(三) 进程控制
  • .NET Framework 的 bug?try-catch-when 中如果 when 语句抛出异常,程序将彻底崩溃
  • .NET NPOI导出Excel详解
  • .NET 动态调用WebService + WSE + UsernameToken
  • .Net 转战 Android 4.4 日常笔记(4)--按钮事件和国际化
  • .NET企业级应用架构设计系列之应用服务器
  • .NET是什么
  • .NET序列化 serializable,反序列化
  • .net之微信企业号开发(一) 所使用的环境与工具以及准备工作
  • [ 隧道技术 ] cpolar 工具详解之将内网端口映射到公网