在Android 中存储数据时经常用SharedPreference, 并且在提交数据时一直用的是Editor的commit(API 1中的方法), 系统用了apply(API 9中的方法),看了方法的介绍, 原来这个方法也是可以提交数据的.

原文地址:https://developer.android.com/reference/android/content/SharedPreferences.Editor.html

apply方法在官方SDK说明如下:

apply

Added in API level 9
void 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.

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 apply(), you can simply call commit() from apply().

commit

Added in API level 1
boolean 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.

这两个方法的区别在于: 
1. apply没有返回值而commit返回boolean表明修改是否提交成功 
2. apply是将修改数据原子提交到内存, 而后异步真正提交到硬件磁盘, 而commit是同步的提交到硬件磁盘,因此,在多个并发的提交commit的时候,他们会等待正在处理的commit保存到磁盘后在操作,从而降低了效率。而apply只是原子的提交到内容,后面有调用apply的函数的将会直接覆盖前面的内存数据,这样从一定程度上提高了很多效率。 
3. apply方法不会提示任何失败的提示。 
由于在一个进程中,sharedPreference是单实例,一般不会出现并发冲突,如果对提交的结果不关心的话,建议使用apply,当然需要确保提交成功且有后续操作的话,还是需要用commit的。

SharedPreference.Editor的 apply 和commit 方法异同相关推荐

  1. 转载 SharedPreference.Editor的apply和commit方法异同

    转载别人的转载:https://blog.csdn.net/jake9602/article/details/18414841 在android 中存储数据时经常用SharedPreference, ...

  2. android方法apply,SharedPreferences的apply和Commit方法的那些坑

    大家都知道,使用SharedPreferences时,用apply方法比commit方法效率更高,但是apply方法其实有一个巨大的坑,容易引发ANR问题. 问题背景 我们做App时,发现一个很奇怪的 ...

  3. 每日一问:谈谈 SharedPreferences 的 apply() 和 commit()

    SharedPreferences灵魂拷问之原理 先来一波灵魂追问: 听说提交要用apply(),为什么? 和commit()什么区别? 跨进程怎么操作? 会堵塞主线程吗? 很着急有替代方案吗? (  ...

  4. JS中的call、apply、bind方法详解

    bind 是返回对应函数,便于稍后调用:apply .call 则是立即调用 . apply.call 在 javascript 中,call 和 apply 都是为了改变某个函数运行时的上下文(co ...

  5. 源码分析SharePreferences的apply与commit的区别

    apply与commit有什么区别 还是从源码分析来说明问题: 接上图标记B1处,看下: 所以这个handler发送消息后,handleMessage会被调用看下: 上图提到的Runnable对象是哪 ...

  6. groupby+(apply+agg+transform)方法的比较

    groupby+(apply+agg+transform)方法的比较 分组函数groupby+(apply+agg+transform)方法的比较 pandas模块给数据处理的能力给予了很大的助力,但 ...

  7. 总结apply和call方法的使用 bind方法 bind方法的使用 函数中的几个成员

    总结apply和call方法的使用 <!DOCTYPE html> <html lang="en"> <head><meta charse ...

  8. 函数的不同的调用方式 函数也是对象 数组的函数调用 apply和call方法的使用

    函数的不同的调用方式 <!DOCTYPE html> <html lang="en"> <head><meta charset=" ...

  9. hibernate mysql autocommit_Hibernate4 中为什么我没有用commit()方法直接用save就存到数据库了?...

    求大神讲解下 Hibernate4 中为什么我没有用commit()方法直接用save就存到数据库了? public class Main { public static void main(Stri ...

最新文章

  1. javascript原理_JavaScript程序包管理器工作原理简介
  2. JAVA Spring Cloud 注册中心 Eureka 相关配置
  3. 大学编译原理试卷考试题
  4. c语言竞赛成绩排序,吧内编程竞赛:成绩公布
  5. mysql log 记录报错 sql语句_MySQL生产库中添加修改表字段引起主从崩溃的问题总结...
  6. 曾断崖式跌落的三星,能否在中国东山再起?
  7. Auto.js 全命令整理(三) 输出专题
  8. 师范生计算机期末考试题目,2015年深圳宝安区计算机教师招聘考试试题[师范生发布]无答案...
  9. 剑指offer——面试题55:字符流中第一个不重复的字符
  10. document.getElementsByName , document.getElementsByTagName ,document.createElement
  11. ios xcode如何在控制台打印frame
  12. SpringBoot数据访问-------------数据缓存
  13. 测试类写法以及几种常用方式
  14. CNN的基本概念、常用的计算公式和pytorch代码
  15. tumblr_如何将Google AdSense添加到您的Tumblr博客
  16. JS 实现网页截屏五种方法
  17. uni-app的editor的富文本编辑器
  18. 聚名:“虎虎酒”商标正在申请中,电视剧《赘婿》引商标注册热潮!
  19. 阿里云ECS服务器跨账号迁移
  20. html 字体模糊,详解css3使用transform出现字体模糊的解决办法

热门文章

  1. android ppt下载地址,Microsoft PowerPoint下载
  2. arduino 土壤温湿度传感器_智慧农业温室大棚环境监测传感器
  3. xp安装mysql5.5.28_Windows XP系统安装MySQL5.5.28图解
  4. 四、Git多人开发:不同人修改了同文件的相同区域如何处理?
  5. 八、Flume的构架,安装和基本使用
  6. 因为高考砸了,所以大学一直在超越
  7. 如何遍历一个JS对象中的所有属性,输出键值对--我居然犯错半个小时
  8. 全面改进Transformer类预训练模型,自然语言任务超越BERT
  9. 基于Conditional Layer Normalization的条件文本生成
  10. 爬虫笔记11Scrapyyield具体使用