iPhone 应用程序中的数据持久性
在开发我的第一个严肃的 iPhone 应用程序时(这意味着我希望将其真正发布到 AppStore),我需要一种好方法来表示我的数据。我(我认为这是愚蠢的)决定使用类来表示我的数据,而不是使用核心数据。
我创建了三个类:MMDot、MMShowMovement 和 MMShow。 MMShowMovement 在数组中保存 MMDot 类的实例,就像 MMShow 保存 MMShowMovement 的实例一样。听起来是一个很好的方法,每个类都有很多与之相关的逻辑,所以我认为类似乎是表示所有这些数据的好方法。
我的问题是,是否有一种简单的方法来保存这些类的实例,以便我可以在重新打开应用程序后恢复数据?我已经使用 NSKeyedArchiver 在 OS X 上制作了应用程序,在 iPhone 上会类似吗?使用 Core Data 重新开始会更容易吗(这些对象非常复杂,尤其是具有大约 15 个实例变量的 MMDot,现在我看看它,也许没那么复杂)?
我在这里真的很茫然。
In the development of my first serious iPhone application (Meaning I'd like to actually get it out on the AppStore), I needed a good way to represent my data. Instead of going with core data, I (stupidly, I think) decided to use classes to represent my data.
I created three classes, MMDot, MMShowMovement, and MMShow. The MMShowMovement holds instances of the MMDot class in an array, as the MMShow holds instances of MMShowMovement. Sounded like a nice way to do it, each class has a lot of logic that has to go with it, so I thought that classes seemed like a good way to go about representing all this data.
My question is, is there an easy way to save the instances of these classes so I can restore the data after the application is reopened? I have made applications on OS X using a NSKeyedArchiver, would it be similar on the iPhone? Would it be easier to start again using Core Data instead (These objects are very complex, especially MMDot with about 15 instance variables, now that I look at it, maybe not THAT complex)?
I'm really at a loss here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您绝对可以使用 NSKeyedArchiver 来保存您的对象。我喜欢这两种方法,决定主要取决于您的应用程序需求。我认为在 CoreData 中重新建模对象并不需要太多工作。在不了解您的应用程序的情况下,我想说,如果您使用简单的模型进行简单的持久化,您可能不需要核心数据,如果您有复杂的关系并且将在应用程序会话期间频繁读取/持久化数据,那么核心数据可能就是这样去。
You can absolutely use NSKeyedArchiver to persist your objects. I'm a fan of either method, the decision mostly lies with your application needs. I don't think it will be much work to re-model your objects in CoreData. Without knowing anything about your application I'd say if you are doing simple persistence with a simple model you may not need Core Data, if you have complex relationships and will be reading/persisting data frequently during an application session Core Data is probably the way to go.
如果您有很多对象实例,那么使用 Core Data 可能会更好 - 它将帮助缓存内容,帮助您进行查询以获取特定的对象集,等等。如果您要保存少量实例,KeyArchiving 确实会更有用,但它也有一些缺点 - 特别是很难跟踪未归档对象的内存泄漏。
Core Data 还可以为您对结果进行分页,这样您就不会在不需要时获取大型数组的主要内容。
If you have a lot of object instances, you are probably better off using Core Data - it will help cache things, help you with queries to get specific sets of objects, and so on. KeyArchiving is really a lot more useful if you are saving off a handful of instances, but have some downsides - in particular it's harder to track down memory leaks from Unarchived objects.
Core Data can also do paging of results for you too, so that you aren't fetching in the entre contents of a large array if you do not need it.
前几天我正在考虑序列化我的类,并选择使用 NSUserDefaults。我只是根据对象为对象的所有内部结构提供一个唯一的键。似乎对我来说工作很快。
在这里写了一篇关于它的博客: http://technolojiadev.blogspot.com/2009 /12/serialize-this.html
I was looking into serializing my classes the other day and went with using NSUserDefaults. I just give all the internals of my objects a unique key based on the object. Seemed to work fast for me.
Wrote a blog about it here: http://technolojiadev.blogspot.com/2009/12/serialize-this.html