SharedPreferences中的commit和apply方法

  • preferences:参数
  • SharedPreferences:共享参数

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

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

Note that when two editors are modifying preferences at the same * time, the last one to call apply wins. * *

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

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

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 * {@link #commit} from apply(). */ void apply();

  • apply方法是没有返回值的
  • 当两个editor同时对preferences对象编辑时,也是最后一个调用apply方法的对象编辑数据
  • apply的提交操作也是原子性的

4. commit和apply方法的区别:

  1. commit和apply虽然都是原子性操作,但是原子的操作不同,commit是原子提交到数据库,所以从提交数据到存在Disk中都是同步过程,中间不可打断。

  2. 而apply方法的原子操作是原子提交的内存中,而非数据库,所以在提交到内存中时不可打断,之后再异步提交数据到数据库中,因此也不会有相应的返回值。

  3. 所有commit提交是同步过程,效率会比apply异步提交的速度慢,但是apply没有返回值,永远无法知道存储是否失败。

  4. 在不关心提交结果是否成功的情况下,优先考虑apply方法。

你可能感兴趣的:(SharedPreferences中的commit和apply方法)