假设我打电话给本地存储,如下所示:
window.localStorage.setItem("key", bigJsonObject);
然后,用户立即关闭其Web浏览器.结果会是什么?
window.localStorage.getItem("key")
bigJsonObject会被部分写入吗?或者整个写入会失败吗?他们有什么方法可以保证不会有部分写入?
解决方法:
参见§4.1 of the “web storage” specification:
The
setItem()
andremoveItem()
methods must be atomic with respect to failure. In the case of failure, the method does nothing. That is, changes to the data storage area must either be successful, or the data storage area must not be changed at all.
但是,(历史上)在这方面存在浏览器错误,e.g. some time before Chrome 21 until some time before Chrome 29.