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之后,才切换状态。

 

android SharedPreferences apply和commit的区别的更多相关文章

  1. SharedPreferences.Editor 的apply()与commit()方法的区别

    commit()的文档 官方文档如下: Commit your preferences changes back from this Editor to the SharedPreferences o ...

  2. Android SharedPreferences中apply和commit的效率差距

    Android SharedPreferences中apply和commit的效率差距 经常看到它俩的速度有差别,apply和commit.到底差距多少,下面做一个统计.   apply commit ...

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

    SharedPreferences 应该是任何一名 Android 初学者都知道的存储类了,它轻量,适合用于保存软件配置等参数.以键值对的 XML 文件形式存储在本地,程序卸载后也会一并清除,不会残留 ...

  4. Android SharedPreferences一般的读写 的用法。

    Android SharedPreferences一般用于轻量级的数据存储,比如用户名和密码等. package com.lixu.testsharepreferences; import andro ...

  5. Android Library和Android APP、Java Library的区别

    Android Library和Android APP.Java Library的区别 Android Library在目录结构上与Android App相同,它能包含构建APP所需的一切(如源代码. ...

  6. 瞬间记住Javascript中apply与call的区别

    关于Javascript函数的apply与call方法的用法,网上的文章很多,我就不多话了.apply和call的作用很相似,但使用方式有区别 apply与call的第一个参数都是一个对象,这个对象就 ...

  7. 改变this指针的apply,call,bind的区别

    apply.call 在 javascript 中,call 和 apply 都是为了改变某个函数运行时的上下文(context)而存在的,换句话说,就是为了改变函数体内部 this 的指向. Jav ...

  8. apply()和call()的区别

    这两个方法的用途都是在特定的作用域中调用函数,实际上等于设置函数体内this对象的值. apply()接收两个参数:一个参数是在其中运行的作用域,另一个是参数数组(可以是Array实例,也可以是arg ...

  9. 【JavaScript】apply和call的区别在哪?

    我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里我做如下笔记,希望和大家 ...

随机推荐

  1. 在Tomcat中部署Java Web应用程序

    在Tomcat中部署Java Web应用程序有两种方式:静态部署和动态部署.在下文中$CATALINA_HOME指的是Tomcat根目录. 一.静态部署      静态部署指的是我们在服务器启动之前部 ...

  2. 一些dos命令

    MS DOS 命令大全 一.基础命令 1 dir 无参数:查看当前所在目录的文件和文件夹. /s:查看当前目录已经其所有子目录的文件和文件夹. /a:查看包括隐含文件的所有文件. /ah:只显示出隐含 ...

  3. thinkphp 使用过程中遇到的一个小函数

    1.实现导出Excel文件,并在导出的文件中显示图片 //导出 public function push(){ $goods_list=M('Dajia')->select(); $data = ...

  4. go语言使用redis —— redigo

    redis的client有好多好多,go语言的client在redis官方有两个推荐,radix和redigo.选择哪一个好呢?确实很纠结,后来掷硬币决定选择redigo了. redis.go.red ...

  5. 两年的坚持,最后还是决定将ISoft开源

    还记得2011年9月份,我在上大四,本来想着考研能上个好点的学校,可我怎么就不愿去自习室上自习.每天晚上睡觉前都告诉自己明天早晨一定早起去上自习,但又每次醒来都不想起床啊,懒,没办法.睡到不想再睡了才 ...

  6. maven3.1安装及配置

    1.首先下载maven3,并安装 2.环境变量配置与JAVA_HOME的配置一样 3.打开MyEclipse preferences>>Maven4MyEclipse>>Mav ...

  7. unity手游之聊天SDK集成与使用一

    手游中都有聊天功能,比如公会,私聊,世界聊天,那么找一个好用,功能强大的SDK的可以节省很多精力,帮助我们提高开发速度与游戏质量. 写本篇博文是为了方便使用这个SDK做聊天模块的程序,避免许多坑,我在 ...

  8. JAVA多线程synchronized详解

    Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 当两个并发线程访问同一个对象object中的这个synchronized(this)同 ...

  9. C# 5.0 TAP 模式下的HTTP Get和Post

    标题有点瘆人,换了工作之后很少写代码了,之前由于签了保密协议,不敢把代码拿出来分享给大家,只能摘抄网上的, 今斗胆拿出来晒晒,跪求指点,直接上代码吧 public class HTTPHelper : ...

  10. spring4+hibernate3

    环境说明:spring4.0+hibernate3 数据库:oracle 连接池:c3p0 项目结构: lib中的jar: 一.配置spring.xml 说明:这里采用的配置模式将hibernateT ...