看的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是没有返回值的,你没法知道它成功还是失败.
为什么说异步执行的那些apply的任务的时间最终会嫁接到主线程上?
QueuedWork.waitToFinish()方法,会等着把这些Runnable都执行完.
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.