简体   繁体   English

如果我有一个包含100个对象的数据列表,并且每个对象都有自己的数据,我应该使用哪种类型的数据存储?

[英]What type of data storage should I use if I have a list of data that contains 100 objects and each object has its own data?

My plan is to display a list of items alphabetically in a table view that has about 100 items. 我的计划是在包含大约100个项目的表格视图中按字母顺序显示项目列表。 Each item has an image, a list of times and a description that the tableview will drill down to. 每个项目都有一张图片,一个时间列表和一个表格视图将深入到的描述。 What I am struggling with is the correct way to store and load this data. 我正在努力的是存储和加载此数据的正确方法。 Some have told me that a plist will be too data heavy and that core data is too new. 有人告诉我,plist数据量太大,而核心数据太新。 Should I just create arrays? 我应该只创建数组吗?

You're not clear about what you intend to do with this data. 您不清楚要使用此数据做什么。 Plists and Core Data are both persistence formats (on disk). Plist和Core Data都是持久性格式(在磁盘上)。 Arrays are an in-memory format (and can also be slapped onto disk, I suppose, if that's what you want to do, but inventing your own binary disk format is only something you should consider very rarely, and certainly not in the case you probably have). 数组是一种内存中的格式(我想,也可以将其拍打到磁盘上,如果那是您想要的,但是发明自己的二进制磁盘格式只是您应该很少考虑的事情,当然在您遇到这种情况时也不应该考虑)可能有)。

In memory, you can probably just use an array (NSArray) and have each element perhaps be an NSDictionary of the other properties relative to that entry. 在内存中,您可能只使用一个数组(NSArray),并使每个元素可能是该条目相对于其他属性的NSDictionary。 That sounds like the model of your MVC design, which you can then hook up to the table view. 听起来像是MVC设计的模型,然后可以将其连接到表视图。

As far as persisting this to disk, it depends on whether 100 items is a fixed amount, a ballpark, or a minimum, etc. Plists (see NSKeyedArchiver) are great for all the data except possibly the raw image data-- you might want to keep those "to the side" as separate image files with filenames in the plist. 至于将其持久化到磁盘上,取决于100个项目是固定数量,固定数量还是最小数量,等等。Plist(请参阅NSKeyedArchiver)适合所有数据,但可能还包括原始图像数据-您可能想要以便将这些文件“保留”为单独的图像文件,并在plist中添加文件名。

I don't know much about Core Data, but it's not that new, and it's not untested, so if it does what you want without much hassle, go for it. 我对Core Data不太了解,但是它不是那么新,并且还没有经过测试,因此,如果它可以轻松完成您想要的操作,那就去做吧。

Serialize it into an Archive using NSCoding Protocol. 使用NSCoding协议将其序列化为存档。 See Guide . 请参阅指南 I'd use an NSArray of business objects implementing NSCoding and then just archive them. 我将使用实现NSCoding的业务对象的NSArray ,然后将其归档。

I usually default to Core Data unless I have a compelling reason not to. 我通常会默认使用Core Data,除非我有令人信服的理由不这样做。 (Of course, I have learned Core Data so that makes it easy for me to this.) (当然,我已经学习了核心数据,因此对我来说很容易。)

Core Data has the following advantages: 核心数据具有以下优点:

  1. It has an editor in which you can create complex object graphs easily 它具有一个编辑器,您可以在其中轻松创建复杂的对象图
  2. It can generate custom classes for you data model objects. 它可以为您的数据模型对象生成自定义类。
  3. The generated classes are easily modified. 生成的类易于修改。
  4. Core Data manages the complexity of adding, deleting and saving objects. 核心数据管理添加,删除和保存对象的复杂性。
  5. Core Data makes persisting an object graph almost invisible. 核心数据使持久保留对象图几乎不可见。
  6. NSFetchedResultsController is custom designed to provide data for tables. NSFetchedResultsController是自定义设计的,用于为表提供数据。

100 objects is a small graph that Core Data can handle easily. 100个对象是一个小图,Core Data可以轻松处理。 It's a lot easier to use Core Data than it is to write custom coders to archive custom objects. 使用Core Data比编写自定义编码器来归档自定义对象要容易得多。 For exmaple, at present, I have an app with over a dozen major entities each with two or three relationships to other entities. 例如,目前,我有一个包含十几个主要实体的应用程序,每个主要实体与其他实体有两三个关系。 Hand coding all that would be a nightmare. 手工编写所有代码将是一场噩梦。

Core Data has something of a steep learning curve especially if you've never worked with object graphs before but if you're planning on writing a lot of Apple platform software, learning it is well worth the time. Core Data的学习曲线有些陡峭,特别是如果您以前从未使用过对象图,但是如果您打算编写许多Apple平台软件,那么学习它是值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM