欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

commit()的文檔

官方文檔如下:

創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供太和網(wǎng)站建設(shè)、太和做網(wǎng)站、太和網(wǎng)站設(shè)計(jì)、太和網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、太和企業(yè)網(wǎng)站模板建站服務(wù),十余年太和做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

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()方法是同步提交到硬件磁盤,因此,在多個(gè)并發(fā)的提交commit的時(shí)候,他們會(huì)等待正在處理的commit保存到磁盤后在操作,從而降低了效率。

  4. apply()沒有返回值.apply()是將修改的數(shù)據(jù)提交到內(nèi)存, 而后異步真正的提交到硬件磁盤.

為什么建議使用apply()替代commit() ?

答:因?yàn)锳ndroid的設(shè)計(jì)人員發(fā)現(xiàn),開發(fā)人員對(duì)commit的返回值不感興趣,而且在數(shù)據(jù)并發(fā)處理時(shí)使用commit要比apply效率低,所以推薦使用apply.

名稱欄目:SharedPreferences.Editor的apply()與commit()方法的區(qū)別
網(wǎng)頁鏈接:http://chinadenli.net/article46/ppgehg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)網(wǎng)站策劃、ChatGPT網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)