核心数据联合查询等效 [英] Core Data Union Query Equivalent

查看:244
本文介绍了核心数据联合查询等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得来自不同实体的数据的并集。我有一些实体(不同种类的标签,例如位置,事件等),我想要一个表视图的数据,显示所有标签(即所有标签实体的联合)。如何使用Core Data对这种用例进行获取请求?



我知道Core Data不是ORM,但如果我上面的解释不好足够,我将解释相应的数据库用例。我有不同的表格事件,位置,人等,我想从这些不同的表的UNION的结果。请记住,UNION连接行而不是列。



一个明显的解决方案是分别从不同的实体获取数据,然后将NSMutableArrays连接在一起。我想知道是否有更有效的方法。

解决方案

没有办法在fetch中获取更多的实体类型请求...



除此之外,您希望Union的实体在您的模型中具有相同的基类( NSManagedObject Tag 的基类,例如:



但是,这将为所有这些实体创建一个统一的表(一个非常稀疏的表,如果类之间的交集



在您的情况下,这可能不可行,因为Person和Location之间没有真正的连接,例如,您可能会认为这将导致性能问题。



另一个解决方案将是(如您所建议)为每个实体创建一个抓取请求。


I want to get the union of data from different entities. I have a number of entities (different kinds of tags e.g. location, events etc) and I want data for a table view that shows "All Tags" (i.e. the union of all tag entities). How do I make a fetch request with Core Data for this kind of a use case?

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. I have different tables e.g. 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.

One obvious solution is to get the data from the different entities separately and then just concatenate together the 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).

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.

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.

The other solution will be (as you suggested) to create a fetch request for each entity.

这篇关于核心数据联合查询等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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