Objective-C 保存数据的最佳选择
我目前正在寻找在 iPhone 应用程序中保存数据的最佳方法;在应用程序打开和关闭之间持续存在的数据。我研究过使用 NSKeyedArchiver 进行归档,并且成功地使其发挥作用。但是,我注意到,如果我尝试保存多个对象,每次保存时它们都会被覆盖。 (本质上,用户将能够创建他/她想要的东西的列表,保存列表,创建更多列表,保存所有列表,然后能够返回并选择其中任何列表以在将来加载日期。)
我听说过 SQLite、Core Data 或使用 .plist 来存储随着时间的推移而持续存在的多个数据数组。有人可以指出我保存数据的最佳方向吗?谢谢!
I'm currently looking for the best way to save data in my iPhone application; data that will persist between opening and closing of the application. I've looked into archiving using a NSKeyedArchiver and I have been successful in making it work. However, I've noticed that if I try to save multiple objects, they keep getting overwritten every time I save. (Essentially, the user will be able to create a list of things he/she wants, save the list, create a few more lists, save them all, then be able to go back and select any of those lists to load at a future date.)
I've heard about SQLite, Core Data, or using .plists to store multiple arrays of data that will persist over time. Could someone point me in the best direction to save my data? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一旦您克服了最初的学习曲线,Core Data 就会非常强大且易于使用。这是一个很好的入门教程 - clicky
Core Data is very powerful and easy to use once you get over the initial learning curve. here's a good tutorial to get you started - clicky
作为 CoreData 的简单而强大的替代品,请考虑 ActiveRecord for Objective-C。 https://github.com/aptiva/activerecord
As an easy and powerful alternative to CoreData, look into ActiveRecord for Objective-C. https://github.com/aptiva/activerecord
我会选择 NSKeyedArchiver。听起来问题是您没有正确组织图表。
从技术上讲,您有一个列表列表,但您只保存内部嵌套列表。
您应该将该列表添加到“超级”列表中,然后对该超级列表进行归档。
CoreData / SQL 似乎与您所描述的有点多。
I'd go with NSKeyedArchiver. Sounds like the problem is you're not organizing your graph properly.
You technically have a list of lists, but you're only saving the inner-nested list.
You should be added the list to a "super" list, and then archiving the super-list.
CoreData / SQL seems a bit much from what you described.
您也可以尝试这个框架。它非常简单且易于使用。
它基于 ActiveRecord 模式,并允许使用迁移、关系、验证等。
它仅使用 sqlite3,没有 CoreData,但您不需要使用原始 sql 或手动创建表。
只需描述您的 iActiveRecord 即可享受。
Also you can try this framework. It's very simple and easy to use.
It's based on ActiveRecord pattern and allow to use migrations, relationships, validations, and more.
It use sqlite3 only, without CoreData, but you don't need to use raw sql or create tables manually.
Just describe your iActiveRecord and enjoy.
您想查看 Ray Wenderlich 的本教程从 CoreData 开始。它很简短,涵盖了 CoreData 的基础知识。
本质上,如果您要存储少量数据,您只想查看 plist。设置或首选项的简单列表。任何比这个更大的东西都会特别围绕性能而崩溃。 iTunesU 上有一个很棒的视频,开发人员在其中LinkedIn 描述了他们在 plist 和 CoreData 之间的性能指标。
归档是可行的,但存储和检索数据需要大量工作,并且会给您带来性能挑战。所以我不会去那里。我会使用 CoreData。如果您了解此堆栈中的对象,那么入门非常简单溢出问题那么你就知道开始所需的一切了。
You want to check out this tutorial by Ray Wenderlich on Getting started with CoreData. Its short and goes over the basics of CoreData.
Essentially you only want to look at plists if you have a small amount of data to store. A simple list of settings or preferences. Anything larger than that and it breaks down specifically around performance. There is a great video on iTunesU where the developers at LinkedIn describe their performance metrics between plists and CoreData.
Archiving works, but is going to be a lot of work to store and retrieve your data, as well as put the performance challenge on your back. So I wouldn't go there. I would use CoreData. Its extremely simple to get started with and if you understand the objects in this stack overflow question then you know everything you need to get going.