ios 最佳实践?- 数组/字典作为核心数据实体属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1562676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:32:12  来源:igfitidea点击:

Best practice? - Array/Dictionary as a Core Data Entity Attribute

objective-cioscore-datansarraynsdictionary

提问by RunLoop

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.

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

回答by Barry Wark

There is no "native" array or dictionary type in Core Data. You can store an NSArrayor an NSDictionaryas a transformable attribute. This will use the NSCodingto serialize the array or dictionary to an NSDataattribute (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 中没有“原生”数组或字典类型。您可以将 anNSArray或 an存储NSDictionary为可转换属性。这将使用 将NSCoding数组或字典序列化为一个NSData属性(并在访问时对其进行适当的反序列化)。这种方法的优点是很容易。缺点是你不能查询数组或字典(它在数据存储中作为 BLOB 存储),如果集合很大,你可能需要将大量数据移入/移出数据存储(如果它是SQLite 数据存储)只是为了读取或修改集合的一小部分。

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.

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

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.

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

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

如果这一切听起来有点令人生畏,那就是。将任意数据硬塞进像 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.

对于结构化数据,如地址,花费时间对实体进行显式建模几乎总是更容易(例如,地址每个部分的属性)。除了避免为字典建模的所有额外代码之外,这还使您的 UI 更容易(绑定将“正常工作”)并使您的验证逻辑等更加清晰,因为其中大部分都可以由 Core Data 处理。

Update

更新

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.

从 OS X 10.7 开始,Core Data 包含一个有序集类型,可以用来代替数组。如果您可以针对 10.7 或更高版本,这是有序(类数组)集合的最佳解决方案。

回答by caleb

I had a similar issue. In my case, I wanted to map an array of strings. I followed Barry's advice and finally got it working. Here is what some of the code looks like (which will hopefully clarify things for anyone else who runs into this)...

我有一个类似的问题。就我而言,我想映射一个字符串数组。我听从了巴里的建议,终于让它发挥了作用。这是一些代码的样子(希望能为遇到此问题的其他人澄清一些事情)......

My Entity looks something like this:

我的实体看起来像这样:

@interface AppointmentSearchResponse : NSManagedObject
@property (nonatomic, retain) NSSet *messages;
@end

My Manage Object Model Code (Core Data) code looks something like this:

我的管理对象模型代码(核心数据)代码如下所示:

NSEntityDescription *entityDescription = [[NSEntityDescription alloc] init];
[entityDescription setName:@"AppointmentSearchResponse"];
[entityDescription setManagedObjectClassName:@"AppointmentSearchResponse"];

NSMutableArray *appointmentSearchResponseProperties = [NSMutableArray array];
NSAttributeDescription *messageType = [[NSAttributeDescription alloc] init];    
[messageType setName:@"messages"];
[messageType setAttributeType:NSTransformableAttributeType];
[appointmentSearchResponseProperties addObject:messageType];

[entityDescription setProperties:appointmentSearchResponseProperties];

So the key items here are:

所以这里的关键项目是:

  • I'm using an NSSet for the property type
  • I'm using NSTransformableAttributeType as the attribute type in the Core Data Managed Object Model.
  • 我使用 NSSet 作为属性类型
  • 我使用 NSTransformableAttributeType 作为核心数据托管对象模型中的属性类型。