核心数据:提取请求中的抽象实体 [英] Core Data: Abstract Entity in Fetch Request

查看:186
本文介绍了核心数据:提取请求中的抽象实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个核心数据模型,其中包含一个名为Animal的抽象实体。然后,我有许多其他实体继承这个抽象实体:狮子,狗,猫等(我不是在开发一个动物园程序,但这个比喻对我解释的问题很好!)

Suppose I have a Core Data model with an abstract entity called "Animal." Then, I have many other entities that inherit from this abstract entity: "Lion", "Dog", "Cat", etc. (I'm not developing a zoo program, but this analogy works well for the issue I'm explaining!)

我想知道的是:我可以通过这样做一次获取所有动物:

What I want to know is: Can I fetch "all animals" at once by doing this:

NSFetchRequest *searchRequest = [[NSFetchRequest alloc] init];
[searchRequest setEntity:[NSEntityDescription entityForName:@"Animal" inManagedObjectContext:aContext]];

NSArray *matchedObjects = [aContext executeFetchRequest:searchRequest error:nil];



我理解有一些NSEntityDescription方法来确定一个实体是否继承自另一个。但是有没有一种快速的方法来抓取所有的实体是一个特定的(抽象)类型---在这种情况下,动物?

I understand there are methods on NSEntityDescription to determine whether an entity inherits from another. But is there a fast way to grab all entities that are of a particular (abstract) type --- in this case, "Animal"?

如果上述方法无效,那么最有效的方法是什么?谢谢!

If the approach above is invalid, what is the most efficient way to go about this? Thanks!

推荐答案

您可以 使用这种方法。

You can definitely use that approach.

从Apple的核心数据编程指南(滚动到文章的底部):

From Apple's Core Data Programming guide (scroll to the bottom section of the article):


如果定义实体继承层次(请参阅实体继承),当您将超级实体指定为获取请求的实体时,该请求将返回超级实体和子实体的所有匹配实例。在某些应用程序中,您可以将超级实体指定为抽象的(请参阅抽象实体)。要获取抽象实体的所有具体子实体的匹配实例,请将抽取规范的实体设置为抽象实体。在抽象实体中描述的域的情况下,如果您使用Graphic实体指定了提取请求,则提取返回Circle,TextArea和Line的匹配实例。

If you define an entity inheritance hierarchy (see "Entity Inheritance"), when you specify a super-entity as the entity for a fetch request, the request returns all matching instances of the super-entity and of sub-entities. In some applications, you might specify a super-entity as being abstract (see "Abstract Entities"). To fetch matching instances of all concrete sub-entities of the abstract entity, you set the entity for fetch specification to be the abstract entity. In the case of the domain described in "Abstract Entities," if you specify a fetch request with the Graphic entity, the fetch returns matching instances of Circle, TextArea, and Line.

这篇关于核心数据:提取请求中的抽象实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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