SharedPreferences.Editor 的apply()与commit()方法的区别
commit()的文档
官方文档如下:
Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.
Note that when two editors are modifying preferences at the same time, the last one to call commit wins.
If you don't care about the return value and you're using this from your application's main thread, consider using apply() instead.
apply()的文档
官方文档如下:
Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.
Note that when two editors are modifying preferences at the same time, the last one to call apply wins.
Unlike commit(), which writes its preferences out to persistent storage synchronously, apply() commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures. If another editor on this SharedPreferences does a regular commit() while a apply() is still outstanding, the commit() will block until all async commits are completed as well as the commit itself.
As SharedPreferences instances are singletons within a process, it's safe to replace any instance of commit() with apply() if you were already ignoring the return value.
You don't need to worry about Android component lifecycles and their interaction with apply() writing to disk. The framework makes sure in-flight disk writes from apply() complete before switching states.
解释说明
- 需要注意的是
commit()
方法是Added in API level 1
的,也就是sdk1
就已经存在了. apply()
方法是Added in API level 9
的.commit()
有返回值,成功返回true
,失败返回false
.commit()
方法是同步提交到硬件磁盘,因此,在多个并发的提交commit的时候,他们会等待正在处理的commit保存到磁盘后在操作,从而降低了效率。apply()
没有返回值.apply()
是将修改的数据提交到内存, 而后异步真正的提交到硬件磁盘.
为什么建议使用apply()替代commit() ?
答:因为Android的设计人员发现,开发人员对commit的返回值不感兴趣,而且在数据并发处理时使用commit要比apply效率低,所以推荐使用apply.
SharedPreferences.Editor 的apply()与commit()方法的区别的更多相关文章
- SharedPreference.Editor的apply和commit方法异同
这两个方法的区别在于: 1. apply没有返回值而commit返回boolean表明修改是否提交成功 2. apply是将修改数据原子提交到内存, 而后异步真正提交到硬件磁盘, 而commit是同步 ...
- SharedPreference.Editor的apply与commit方法不同之处
定义: void apply boolean commit; 相同:二者都是提交修改的数据 手机里的文件存放在/data/data/<package_name>/shared_prefs ...
- SharedPreference.Editor的apply跟commit方法的異同
相同点: 1.二者都可提交preference的修改数据 2.二者都是原子操作 区别: 1.apply没有返回值而commit返回boolean表明修改是否提交成功 2.apply是将修改数据原子提交 ...
- 关于javascript中apply()和call()方法的区别
如果没接触过动态语言,以编译型语言的思维方式去理解javaScript将会有种神奇而怪异的感觉,因为意识上往往不可能的事偏偏就发生了,甚至觉得不可理喻.如果在学JavaScript这自由而变幻无穷的语 ...
- javascript中apply()和call()方法的区别
一.方法的定义 call方法: 语法:call(thisObj,Object)定义:调用一个对象的一个方法,以另一个对象替换当前对象.说明:call 方法可以用来代替另一个对象调用一个方法.call ...
- JavaScript的apply和call方法及其区别
参考资料: http://blog.csdn.net/myhahaxiao/article/details/6952321 apply和call能“劫持”其他对象的方法来执行,其形参如下: apply ...
- js中的call()、apply()和bind()方法的区别
call(thisObj,param1,param2....)方法:调用一个对象的方法,用另外的对象去替换当前对象. 下面给出一个例子: function add(a,b){ return a+b; ...
- js的call() ,apply() 两种方法的区别和用法,最白话文的解释,让枯燥滚粗!
百度了一圈calll()函数和apply()函数,感觉还是糊里糊涂 正好我前几天刚又重新翻了一遍 那本 600多页 的圣经书,我习惯时不时的去打下基础,只是为了用来装逼,给人讲解....(我是有多蛋疼 ...
- javascript中apply()和call()方法及区别
call()和apply()方法 1.方法定义 call方法: 语法:obj.call(thisObj, arg1, arg2, ...); 定义:调用一个对象的一个方法,以另一个对象替换当前对象. ...
随机推荐
- RadioButton(单选按钮)文字在按钮的左边
<RadioButton style="@style/CustomCheckboxTheme" android:layout_width="fill_parent& ...
- animation_Frame动画图片轮播
我们刚接触的时候想弄一个轮播图片的一个小案例,但一开始我们以为和以前写java一样,要写一下方法,逻辑:但今天你学了这个Frame动画就可以轻松搞定!下面我们来看看这个Frame是怎么实现的. 第一步 ...
- [spring源码学习]三、IOC源码——自定义配置文件读取
一.环境准备 在文件读取的时候,第9步我们发现spring会根据标签的namespace来选择读取方式,联想spring里提供的各种标签,比如<aop:xxx>等应该会有不同的读取和解析方 ...
- 【算法杂谈】Miller-Rabin素性测试算法
额,我们今天来讲一讲Miller-Rabin素性测试算法. 读者:怎么又是随机算法!!!(⊙o⊙)… [好了,言归正传] [费马小定理] 费马小定理只是个必要条件,符合费马小定理而非素数的数叫做Car ...
- C#版 Winform界面 Socket编程 Client客户端
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- mac homebrew的用法
与 MacPorts 类似,OS X 下还有款包管理工具为 Homebrew,安装方法也很简单. ruby -e "$(curl -fsSL https://raw.github.com/H ...
- RStudio技巧02_Extract Function
RStudio 可以在 source 编辑器中分析一组选择的代码,并自动将其转化成再次使用的函数.任何选择中的"free"变量( 选择引用对象但不创建)将转化为函数参数. (也可使 ...
- 整理 PHPstorm实用个人配置,修改调整个性化快捷键,修改使用phpstorm创建的模板的默认注释:
对你有助请点赞,请顶------送人玫瑰,手留余香! 1:58 2016/3/12 整理PHPstorm实用个人配置,修改调整个性化快捷键,修改使用phpstorm创建的模板的默认注释: PHPsto ...
- 步骤进度条 css
用css写一个简单的步骤进度条 html代码: <h4>南京游玩</h4> <ul class="step-list"> <li> ...
- 安装windows服务批处理代码
批处理是DOS时代比较常用的方法之一,目前来说也是一种高效的方法,复制代码到文本文件中,保存并修改文件扩展名为“*.bat”. 安装windows服务批处理代码如下: @echo off set fi ...