最佳实践? - 数组/字典作为核心数据实体属性 [英] Best practice? - Array/Dictionary as a Core Data Entity Attribute

查看:156
本文介绍了最佳实践? - 数组/字典作为核心数据实体属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Core Data的新手。我注意到集合类型不可用作属性类型,并想知道什么最有效的方式是存储数组/字典类型数据作为一个属性(例如组成一个地址,如街道,城市等的元素不需要单独的实体,并且比单独的属性/字段更方便地存储为字典/数组)。谢谢。

I am new to Core Data. I have noticed that collection types are not available as attribute types and would like to know what the most efficient way is of storing array/dictionary type data as an attribute (e.g. the elements that make up an address like street, city, etc. does not require a separate entity and is more conveniently stored as a dictionary/array than separate attributes/fields). Thank you.

推荐答案

Core Data中没有native数组或字典类型。您可以将 NSArray NSDictionary 存储为可转换属性。这将使用 NSCoding 将数组或字典序列化为 NSData 属性(并在访问时适当地反序列化)。这种方法的优点是它很容易。缺点是你不能查询数组或字典(它在数据存储中存储为BLOB),如果集合很大,你可能需要将大量数据移入/移出数据存储(如果它是一个SQLite数据存储)只是读取或修改集合的一小部分。

There is no "native" array or dictionary type in Core Data. You can store an NSArray or an NSDictionary as a transformable attribute. This will use the NSCoding to serialize the array or dictionary to an NSData attribute (and appropriately deserialize it upon access). The advantage of this approach is that it's easy. The downside is that you can't query into the array or dictionary (it's stored as a BLOB in the data store) and if the collections are large, you may have to move a lot of data to/from the data store (if it's an SQLite data store) just to read or modify a small part of the collection.

另一种方法是使用Core Data一对多的关系来建模数组的语​​义或字典集合。数组更容易,所以让我们开始吧。核心数据对多关系真正建模一个集合,所以如果你需要数组一样的功能,你必须排序集合(使用fetched属性是一个方便的方法做到这一点)或添加一个额外的索引属性到实体它存储数组项并自己管理索引。如果要存储齐次数组(所有条目都是相同类型),那么很容易为数组实体建立实体描述的模型。如果没有,你必须决定是否使用transformable属性存储项目数据或创建一个项目实体家族。

The alternative is to use Core Data to-many relationships to model the semantics of the array or dictionary collection. Arrays are easier, so lets start with that. Core Data to-many relationships are really modelling a set, so if you need array-like functionality, you have to either sort the set (using a fetched property is a convenient way to do this) or add an extra index attribute to the entity that stores the array items and manage the indexes yourself. If you are storing a homogeneous array (all entries are the same type), it's easy to model the entity description for the array entities. If not, you'll have to decide whether to use a transformable attribute to store the item data or create a family of item entities.

建模字典可能需要一个一对多关系存储一个键和一个值的一组实体。键和值都类似于数组的项实体,如上所述。因此,它们可以是本机类型(如果您提前知道它们),一个可转换的属性或与一个类型特定实体家族的实例的关系。

Modeling a dictionary will likely require a to-many relationship to a set of entities that stores a key and a value. Both key and value are analogous to the item entity for the array, described above. So they could either be native types (if you know them ahead of time), a transformable attribute or a relationship to an instance from a family of type-specific entities.

如果这一切听起来有点令人生畏,它是。将任意数据推入像Core Data这样的依赖于模式的框架是很困难的。

If this all sounds a bit daunting, it is. Shoehorning arbitrary data into a schema-dependent framework like Core Data is tough.

对于结构化数据,如地址,几乎总是更容易花费时间对实体建模例如地址的每个部分的属性)。除了避免所有额外的代码建模字典,这使得你的UI更容易(绑定将只是工作)和你的验证逻辑等更清楚,因为它的大部分可以由Core Data处理。

For structured data, like addresses, it's almost always easier to spend the time modeling the entities explicitly (e.g. an attribute for each part of the address). Besides avoiding all the extra code to model a dictionary, this makes your UI easier (bindings will "just work") and your validation logic etc. much clearer since much of it can be handled by Core Data.

更新

从OS X 10.7开始,Core Data包括一个可以在位的数组。如果你可以定位10.7或更高版本,这是有序(数组类)集合的最佳解决方案。

As of OS X 10.7, Core Data includes an ordered set type which can be used in place of an array. If you can target 10.7 or later, this is the best solution for ordered (array-like) collections.

这篇关于最佳实践? - 数组/字典作为核心数据实体属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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