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.
*
* <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方法的區別:
commit和apply雖然都是原子性操作,但是原子的操作不同,commit是原子提交到資料庫,所以從提交資料到存在Disk中都是同步過程,中間不可打斷。
而apply方法的原子操作是原子提交的記憶體中,而非資料庫,所以在提交到記憶體中時不可打斷,之後再非同步提交資料到資料庫中,因此也不會有相應的返回值。
所有commit提交是同步過程,效率會比apply非同步提交的速度慢,但是apply沒有返回值,永遠無法知道儲存是否失敗。
在不關心提交結果是否成功的情況下,優先考慮apply方法。
相關文章
- 傻傻分不清楚系列(一):SharedPreferences的commit和apply方法MITAPP
- JS中的call()和apply()方法JSAPP
- 【轉】JS中的call()和apply()方法JSAPP
- JS中的call()方法和apply()方法用法總結JSAPP
- JS中的call、apply、bind方法JSAPP
- js中call,apply和bind方法的區別和使用場景JSAPP
- JavaScript中的call()和apply()JavaScriptAPP
- Android中SharedPreferences使用方法介紹Android
- 理解JS中的call、apply、bind方法(********************************************************JSAPP
- 快速掌握javascript的apply()和call()方法JavaScriptAPP
- JS中apply和call的用法JSAPP
- JavaScript中call,apply,bind方法的總結。JavaScriptAPP
- JavaScript中call,apply,bind方法的總結JavaScriptAPP
- android中sharedPreferences的用法Android
- 對javascript中的call()和apply()的理解JavaScriptAPP
- 徹底理解了call()方法,apply()方法和bind()方法APP
- 重寫JS中的apply,call,bind,new方法JSAPP
- 談談JavaScript中的call、apply和bindJavaScriptAPP
- 淺談JavaScript中的apply、call和bindJavaScriptAPP
- python中的一個現象,db.commit和db.commit()PythonMIT
- pandas | 詳解DataFrame中的apply與applymap方法APP
- CROSS APPLY 和outer apply 的區別ROSAPP
- SharePreferences原始碼分析(commit與apply的區別以及原理)原始碼MITAPP
- 你已經用 SharedPrefrence 的 apply() 替換 commit() 了嗎?APPMIT
- 深入理解Android中的SharedPreferencesAndroid
- 瞭解 Android API 中的 SharedPreferencesAndroidAPI
- Angular的scope. apply和CRM WebClient UI的modify方法AngularAPPWebclientUI
- 簡單快速理解js中的this、call和applyJSAPP
- call()和apply()方法使用程式碼例項APP
- angularJS之$watch、$digest和$apply方法AngularJSAPP
- 快速理解JavaScript中call和apply原理JavaScriptAPP
- 優雅的陣列降維——Javascript中apply方法的妙用陣列JavaScriptAPP
- Scala中apply的用法APP
- js中的call、applyJSAPP
- Docker映象提交命令commit的工作原理和使用方法DockerMIT
- 模擬js中的call、apply和bind的實現JSAPP
- Reflect.apply() 方法APP
- JS中改變this的指向 call、apply 和 bind 的區別JSAPP