SharedPreferences.Editor 的apply()與commit()方法的區(qū)別

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.

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-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures. If another editor on this SharedPreferences does 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 SharedPreferences instances 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.

解釋說明
1做院、需要注意的是commit()方法是Added in API level 1的,也就是sdk1就已經(jīng)存在了.
2、apply()方法是Added in API level 9的.
3讲岁、commit()有返回值,成功返回true,失敗返回false.commit()方法是同步提交到硬件磁盤屁擅,因此,在多個并發(fā)的提交commit的時候遣疯,他們會等待正在處理的commit保存到磁盤后在操作傲醉,從而降低了效率乌助。/4、apply()沒有返回值.apply()是將修改的數(shù)據(jù)提交到內(nèi)存, 而后異步真正的提交到硬件磁盤.
為什么建議使用apply()替代commit() ?
答:因為Android的設(shè)計人員發(fā)現(xiàn),開發(fā)人員對commit的返回值不感興趣,而且在數(shù)據(jù)并發(fā)處理時使用commit要比apply效率低,所以推薦使用apply.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末很澄,一起剝皮案震驚了整個濱河市京闰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌痴怨,老刑警劉巖忙干,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異浪藻,居然都是意外死亡捐迫,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進(jìn)店門爱葵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來施戴,“玉大人,你說我怎么就攤上這事萌丈≡藁” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵辆雾,是天一觀的道長肪笋。 經(jīng)常有香客問我,道長度迂,這世上最難降的妖魔是什么藤乙? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮惭墓,結(jié)果婚禮上坛梁,老公的妹妹穿的比我還像新娘。我一直安慰自己腊凶,他們只是感情好划咐,可當(dāng)我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著钧萍,像睡著了一般褐缠。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上划煮,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天送丰,我揣著相機(jī)與錄音,去河邊找鬼弛秋。 笑死器躏,一個胖子當(dāng)著我的面吹牛俐载,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播登失,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼遏佣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了揽浙?” 一聲冷哼從身側(cè)響起状婶,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎馅巷,沒想到半個月后膛虫,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡钓猬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年稍刀,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片敞曹。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡账月,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出澳迫,到底是詐尸還是另有隱情局齿,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布橄登,位于F島的核電站抓歼,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏拢锹。R本人自食惡果不足惜锭部,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望面褐。 院中可真熱鬧,春花似錦取胎、人聲如沸展哭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽匪傍。三九已至,卻和暖如春觉痛,著一層夾襖步出監(jiān)牢的瞬間役衡,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工薪棒, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留手蝎,地道東北人榕莺。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像棵介,于是被迫代替她去往敵國和親钉鸯。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容