SharedPreference.Editor的apply和commit方法區(qū)別

轉(zhuǎn)載請標(biāo)明出處:
http://m.itdecent.cn/p/7be25bfb4cb4
本文出自:
http://m.itdecent.cn/u/a1251e598483

在以往的開發(fā)中存儲數(shù)據(jù)時(shí)經(jīng)常用SharedPreference, 并且在提交數(shù)據(jù)時(shí)一直用的是Editor的commit方法,今天看郭神的第一行代碼中舉例子用的都是apply。所以就想弄清楚,SharedPreference.Editor的apply和commit到底有什么區(qū)別。
下面是查的官方文檔

apply

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-memorySharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures. If another editor on thisSharedPreferencesdoes 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 SharedPreferencesinstances 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.

翻譯 :在編輯中調(diào)用 Editor 的提交方法 apply(),這將原子地執(zhí)行所請求的修改,替換當(dāng)前在SharedPreferences中的任何內(nèi)容。
注意:當(dāng)在同一時(shí)間有兩個(gè) editors 修改 preferences的內(nèi)容時(shí),以最后一個(gè)修改的為準(zhǔn)。
與 commit()方法不同的是 commit()方法是同步地儲存在磁盤上,apply()是先儲存在內(nèi)存中,然后異步的儲存在磁盤上且不會有任何失敗的提示;如果apply()異步的內(nèi)容尚未儲存帶磁盤上,另一個(gè)editor調(diào)用了commit()同步提交儲存,這個(gè)操作將會等待另一個(gè)commit()操作完成后才會真正執(zhí)行。
由于SharedPreferences 在一個(gè)進(jìn)程中是單例的,所以如果你不關(guān)心commit()成功與否的,完全可以用apply()來代替。
你不需要擔(dān)心Android組件生命周期及其與apply()寫入磁盤的交互。該框架確保來自apply()的在線磁盤寫入在切換狀態(tài)之前完成。

commit

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.

翻譯:在編輯中調(diào)用 Editor 的提交方法 apply(),這將原子地執(zhí)行所請求的修改,替換當(dāng)前在SharedPreferences中的任何內(nèi)容。
注意:當(dāng)在同一時(shí)間有兩個(gè) editors 修改 preferences的內(nèi)容時(shí),以最后一個(gè)修改的為準(zhǔn)。

如果你在主線程上調(diào)用此方法,且不關(guān)心儲存成功與否的返回值,你可以直接使用 appply() 。

兩個(gè)方法區(qū)別:

  1. apply沒有返回值而commit有返回表明修改是否成功
  2. apply是將修改數(shù)據(jù)原子提交到內(nèi)存, 而后異步真正提交到硬件磁盤, 而commit是同步的提交到硬件磁盤,因此,在多個(gè)并發(fā)的提交commit的時(shí)候,他們會等待正在處理的commit保存到磁盤后在操作,從而降低了效率。而apply只是原子的提交到內(nèi)容,后面有調(diào)用apply的函數(shù)的將會直接覆蓋前面的內(nèi)存數(shù)據(jù),這樣從一定程度上提高了很多效率。
  3. apply方法不會提示任何失敗的提示。
    由于在一個(gè)進(jìn)程中,sharedPreference是單實(shí)例,一般不會出現(xiàn)并發(fā)沖突,如果對提交的結(jié)果不關(guān)心的話,建議使用apply()。

詳細(xì)內(nèi)容可以查看官方參考 :
https://developer.android.google.cn/reference/android/content/SharedPreferences.Editor.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容