中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2020-07-14 23:12:54 來源:網絡 閱讀:430 作者:鄒君安 欄目:網絡安全

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就已經存在了.

  2. apply()方法是Added in API level 9的.

  3. commit()有返回值,成功返回true,失敗返回false.commit()方法是同步提交到硬件磁盤,因此,在多個并發的提交commit的時候,他們會等待正在處理的commit保存到磁盤后在操作,從而降低了效率。

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

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

答:因為Android的設計人員發現,開發人員對commit的返回值不感興趣,而且在數據并發處理時使用commit要比apply效率低,所以推薦使用apply.


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

靖宇县| 钟祥市| 虎林市| 繁昌县| 吉木萨尔县| 阜平县| 长顺县| 拜泉县| 城口县| 河北区| 芜湖市| 岑溪市| 墨江| 鞍山市| 得荣县| 朝阳区| 宁海县| 湖北省| 天柱县| 六盘水市| 利辛县| 桓台县| 涟源市| 深圳市| 蒙山县| 罗山县| 安义县| 武强县| 敦煌市| 襄城县| 宁海县| 兰坪| 鹤山市| 额尔古纳市| 石家庄市| 宜兰市| 肥城市| 柳林县| 黄冈市| 麦盖提县| 邵阳市|