- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
阅读 SharedPreferences.Editor.apply() 上的文档我的理解是 apply()
的行为与 commit()
完全相同,但对算法进行了优化以防止 ANR 等。
文档明确指出用 apply()
替换 commit()
是安全的,并且 lint 鼓励我们这样做。
所以问题是为什么他们设计了 apply()
而没有修改 commit()
呢?
最佳答案
So the question is why they have devised the apply() and haven't modified commit() instead?
commit()
返回一个 boolean
。引用 the documentation , boolean
是:
true if the new values were successfully written to persistent storage.
如果 commit()
被修改为异步的,那么它不能返回一个 boolean
来指示成功的写操作,因为在返回值的时候,写入状态尚不清楚。
与此同时,他们不能修改 commit()
以返回 void
或某些 Future
之类的东西,因为那样会破坏二进制文件与编写时认为 commit()
将返回 boolean
的应用程序兼容。使用现有 commit()
的现有应用程序将失败并返回 VerifyError
或当尝试使用非boolean
返回值。
此外,Java 不允许你有两个同名不同返回值的方法。所以他们不可能有两个版本的commit()
,除非他们人为地添加了一个参数什么的。
因此,他们创建了 apply()
。
关于android - 为什么发明了新的 apply() 而不是修改 commit() 行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28365268/
我是一名优秀的程序员,十分优秀!