android commit apply,关于SharedPreference的commit和apply和ANR
看的SharedPreferencesImpl.java 辦法呢 有人是不用apply直接自己異步commit. 也有人用反射把那個QueuedWork的sFinishers變量弄成empty.
文檔上說的commit和apply, Unlike commit,which writes its preference out to persistent storage synchronously, apply commits its changes to the in-memory immediately but starts an asynchronous commit to disk,and you won't be notified of any failures. commit是有一個boolean返回值的,如果是true表示成功寫入persistent storage. 但是apply是沒有返回值的,你沒法知道它成功還是失敗.
為什么說異步執(zhí)行的那些apply的任務(wù)的時間最終會嫁接到主線程上?
QueuedWork.waitToFinish()方法,會等著把這些Runnable都執(zhí)行完.
QueuedWork的文檔里有這么一句話 This was created for writing SharedPreference edits out asynchronously so we'd have a mechanism to wait for the writes in Activity.onPause and similar places, but we may use this mechanism for other things in the future.
然后就猜是不是Activity的onPause()方法做了什么處理 然后不是 然后再猜ActivityThread 就看到了
ActivityThread.java的多幾個地方比如handlePauseActivity()方法里面就有一句 QueuedWork.waitToFinish(); 這里是一定會等著那些異步的apply提交完才往后走的,所以可能ANR.
辦法呢 有人是不用apply直接自己異步commit. 也有人用反射把那個QueuedWork的sFinishers變量弄成empty.
總結(jié)
以上是生活随笔為你收集整理的android commit apply,关于SharedPreference的commit和apply和ANR的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dnf登录TP条然后那个黑屏闪退怎么解决
- 下一篇: html5中折叠面板,Ant Desig