如果应用程序在10秒内停止,NSUserDefaults丢失新保存的数据 [英] NSUserDefaults Lose Newly Saved Data if App Killed Within 10 Seconds

查看:1594
本文介绍了如果应用程序在10秒内停止,NSUserDefaults丢失新保存的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种更快的方式来保存用户偏好比NSUserDefaults。我发现,如果应用程序在写入NSUserDefaults大约10秒内被杀死,它将不会永久保存。我使用默认值保存路径自定义铃声,路径自定义图像,地图坐标,基本上只是用户定义的首选项。使用核心数据是更好的选择吗? SQLite?

I'm looking for a faster way to save user preferences than the NSUserDefaults. I've found that if the app is killed within around 10 seconds of writing to NSUserDefaults, it will not be saved permanently. I use the defaults to save paths to custom ring tones, paths to custom images, map coordinates, and basically just user defined preferences. Is using core data the better option? SQLite? What's accepted as the fastest and most lightweight?

推荐答案

你需要确保调用synchronized来立即保存数据。 p>

You need to be sure to call synchronize to save the data immediately.

[[NSUserDefaults standardUserDefaults] synchronize];

从Apple的类别参考


由于此方法会以周期性间隔自动调用,
只有在无法等待自动
同步时才使用此方法(例如,如果您的应用程序退出)或
,如果您要更新用户默认值为磁盘上的内容,即使
您没有进行任何更改。

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设计为存储非常少量的数据(对于首选项),如切换开关的状态等。你可以逃避存储图像和铃声的路径,但这不是完全可取的。

And to answer your second question, it really depends on how much data you want to store. NSUserDefaults is designed to store very small amounts of data (for preferences) like the state of a toggle switch, etc. You can get away with storing the paths to images and ring tones here but it isn't exactly advisable. By that I mean you can, but probably shouldn't.

核心数据是一个更好的方法,如果你计划存储许多这些路径,因为它是非常可扩展的,并且执行得很好。因此,总的来说,如果你需要存储大量的数据,用户Core Data,或者作为另一个替代存储在文档目录中plist中的路径。

Core Data is a much better approach if you plan on storing many of these paths as it is very scalable and performs very well. So overall, if you need to store a lot of data, user Core Data, or as another alternative store the paths in a plist in the documents directory.

这篇关于如果应用程序在10秒内停止,NSUserDefaults丢失新保存的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆