gpt4 book ai didi

ios - 如果应用程序在 10 秒内被杀死,NSUserDefaults 会丢失新保存的数据

转载 作者:可可西里 更新时间:2023-11-01 03:37:33 25 4
gpt4 key购买 nike

我正在寻找一种比 NSUserDefaults 更快的方式来保存用户首选项。我发现如果应用程序在写入 NSUserDefaults 后大约 10 秒内被终止,它将不会被永久保存。我使用默认值来保存自定义铃声的路径、自定义图像的路径、 map 坐标,基本上只是用户定义的首选项。使用核心数据是更好的选择吗?数据库?什么被认为是最快和最轻量级的?

最佳答案

您需要确保立即调用同步以保存数据。

[[NSUserDefaults standardUserDefaults] synchronize];

来自 Apple 的 class reference :

Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.

然后回答您的第二个问题,这实际上取决于您要存储多少数据。 NSUserDefaults 旨在存储非常少量的数据(用于首选项),例如拨动开关的状态等。您可以在此处存储图像和铃声的路径,但不完全建议这样做。我的意思是你可以,但可能不应该。

如果您计划存储许多这样的路径,Core Data 是一种更好的方法,因为它具有很强的可扩展性和性能。因此,总的来说,如果您需要存储大量数据,请使用 Core Data,或者作为另一种选择,将路径存储在文档目录中的 plist 中。

关于ios - 如果应用程序在 10 秒内被杀死,NSUserDefaults 会丢失新保存的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13980968/

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