简体   繁体   English

等效核心数据联合查询

[英]Core Data Union Query Equivalent

I want to get the union of data from different entities. 我想获得来自不同实体的数据联合。 I have a number of entities (different kinds of tags eg location, events etc) and I want data for a table view that shows "All Tags" (ie the union of all tag entities). 我有许多实体(不同种类的标记,例如位置,事件等),并且我想要显示“所有标记”的表视图的数据(即所有标记实体的并集)。 How do I make a fetch request with Core Data for this kind of a use case? 对于此类用例,如何使用Core Data发出获取请求?

I know that Core Data is not an ORM but if my explanation above was not good enough, I will explain the corresponding database use case. 我知道Core Data不是ORM,但是如果我上面的解释不够好,我将解释相应的数据库用例。 I have different tables eg events, locations, people etc and I would like to UNION the results from these different tables. 我有不同的表,例如事件,位置,人物等,我希望从这些不同的表中获得结果。 Remember that a UNION concatenates the rows and not columns. 请记住,UNION连接行而不是列。

One obvious solution is to get the data from the different entities separately and then just concatenate together the NSMutableArrays. 一种明显的解决方案是分别从不同实体获取数据,然后将NSMutableArrays连接在一起。 I am wondering if there is a more efficient way. 我想知道是否有更有效的方法。

There is no way to fetch more then one entity type in a fetch request ... 在获取请求中,无法获取一种以上的实体类型...

UNLESS, the entities you like to 'Union' have the same base class in your model (other than NSManagedObject ). 除非,您希望“联合”的实体在模型中具有相同的基类( NSManagedObject除外)。

So if all your entities were to inherit from a base class named Tag for example, you would be able to fetch all of them together. 因此,例如,如果您所有的实体都继承自名为Tag的基类,则可以将所有实体一起获取。

This however, will create a unified table for all these entities (a very sparse table if the intersection between the classes is small) in the actual database file. 但是,这将为实际数据库文件中的所有这些实体创建一个统一的表(如果类之间的交集很小,则为非常稀疏的表)。

In your case this might not be feasible as there is no real connection between 'Person' and 'Location' for instance, or you might decide that this will cause a performance issue. 在您的情况下,这可能不可行,因为例如“ Person”和“ Location”之间没有真正的联系,或者您可能决定这将导致性能问题。

The other solution will be (as you suggested) to create a fetch request for each entity. 另一个解决方案是(如您建议的那样)为每个实体创建获取请求。

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

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