gpt4 book ai didi

java - SharedPreferences.Editor apply() 失败

转载 作者:太空宇宙 更新时间:2023-11-03 10:32:46 25 4
gpt4 key购买 nike

正如文档所说,

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.

这是否意味着,如果出现某些故障(例如可用空间不足),我的 SharedPreferences 将恢复到以前的状态,并且不会创建任何异常?我的应用会崩溃吗?

最佳答案

如这里所见 here ,在将更改写入文件之前,SharedPreferences 首先尝试创建一个备份文件

如果成功:

Attempt to write the file, delete the backup and return true as atomically as possible. If any exception occurs, delete the new file; next time we will restore from the backup.

因此,不会抛出任何异常并且 SharedPreferences 将返回到以前的状态(因为它在将更改写入磁盘之前进行了备份)。 (尽管您不会收到任何失败通知)。

关于java - SharedPreferences.Editor apply() 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56447622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com