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

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

问题描述

我是 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 中没有原生"数组或字典类型.您可以将 NSArrayNSDictionary 存储为可转换属性.这将使用 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 的多对多关系来对数组或字典集合的语义进行建模.数组更容易,所以让我们从它开始.Core Data 对多关系实际上是对集合进行建模,因此如果您需要类似数组的功能,则必须对集合进行排序(使用获取的属性是一种方便的方法)或向实体添加额外的索引属性存储数组项并自己管理索引.如果您要存储同构数组(所有条目都是相同类型),则很容易对数组实体的实体描述进行建模.如果没有,您就必须决定是使用可转换属性来存储商品数据还是创建一系列商品实体.

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天全站免登陆