SharedPreferences中的commit和apply方法

weixin_34162695發表於2016-04-19
  • 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.
 *
 * <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.
 *
 * @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.
*
* <p>Note that when two editors are modifying preferences at the same
* time, the last one to call apply wins.
*
* <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.
*
* <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.
*
* <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.
*
* <p class='note'>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 <code>apply()</code>, you can simply call
* {@link #commit} from <code>apply()</code>.
*/
void apply();
  • apply方法是沒有返回值的
  • 當兩個editor同時對preferences物件編輯時,也是最後一個呼叫apply方法的物件編輯資料
  • apply的提交操作也是原子性的

4. commit和apply方法的區別:

  1. commit和apply雖然都是原子性操作,但是原子的操作不同,commit是原子提交到資料庫,所以從提交資料到存在Disk中都是同步過程,中間不可打斷。

  2. 而apply方法的原子操作是原子提交的記憶體中,而非資料庫,所以在提交到記憶體中時不可打斷,之後再非同步提交資料到資料庫中,因此也不會有相應的返回值。

  3. 所有commit提交是同步過程,效率會比apply非同步提交的速度慢,但是apply沒有返回值,永遠無法知道儲存是否失敗。

  4. 在不關心提交結果是否成功的情況下,優先考慮apply方法。

相關文章