核心数据:3表连接? [英] Core Data: 3 table join?

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

问题描述

我知道Core Data不是数据库,有很多区别。这是一个吗?



在数据库中,我通常会有以下



A - >> B - >> C



A有很多B,其中有许多C。



给我所有有c.attr ='X'的A很容易写成:

  select a from a,b, c其中a.id = b.aid和b.id = c.bid和c.attr ='X'


$ b b

在Core Data中,我也想这样做,但使用一个谓词,如:

  NSPredicate * predicate = 
[NSPredicate predicateWithFormat:@ANY bs.cs.attr =%@,X];
[frequentest setEntity:entityA];
[frequentest setPredicate:predicate];

这样做会导致错误:
'NSInvalidArgumentException',reason:'multiple to-many keys不允许在这里'



我正确的解释是什么数据库调用多表联接有限制?



我在Google上搜索,找不到一个确定的答案。



此查询的当前解决方案如下:

  NSPredicate * predicate = 
[NSPredicate predicateWithFormat:@ANY cs.attr =%@,X];
...
NSArray * bs = //执行fetch
for(B * b in bs){
//将ba添加到数组
}
// return array

有更好的方法吗?提前感谢您的考虑。

解决方案

您向后退。



首先,您不需要Core Data中的链接ID,因为所有相关对象都已通过关系关联。这意味着不需要像这样的结构,其中a.id = b.aid和b.id = c.bid



其次,通常为接收定义测试的实体设置fetch实体。在这种情况下,这是 c.attr =X所以,你设置您的fetch实体 C 谓词应该看起来像:

  NSPredicate * p = [NSPredicate predicateWithFormat:@attr =%@,xValue]; 

这将返回一个数组 C 满足测试的实例。然后找到任何特定的 B A 只是为每个 C



如果你的逆关系是一个例如A←→B←C,你只需问每个 C ba 的值:

  AObject * anA = aCinstance.ba; 

重要的是要记住,你没有在这里处理表。你正在处理一个对象图。您将抓取设置为特定实体,然后遍历过滤的实体关系。


I know Core Data is not a database and there are many differences. Is this one?

In a database, I would commonly have the following

A ->> B ->> C

"A" has many "B" which has many "C"

The query, "Give me all A's which have c.attr = 'X' is easily written like:

select * from a, b, c where a.id = b.aid and b.id = c.bid and c.attr = 'X'

In Core Data, I would like to do the same, but using a predicate like:

NSPredicate *predicate = 
  [NSPredicate predicateWithFormat:@"ANY bs.cs.attr = %@", "X"];
[frequest setEntity:entityA];
[frequest setPredicate:predicate];

Doing this results in the error: 'NSInvalidArgumentException', reason: 'multiple to-many keys not allowed here'

Am I correct to interpret to mean there is a limitation on what databases call multi-table joins?

I googled around and couldn't find a definitive answer.

My current solution to this query looks like:

NSPredicate *predicate = 
  [NSPredicate predicateWithFormat:@"ANY cs.attr = %@", "X"];
...
NSArray *bs = //execute fetch
for (B *b in bs) {
  //add b.a into an array
}
//return array

Is there a better way? Thanks in advance for the consideration.

解决方案

Your going about this backwards.

First of all, you don't need the linking ids in Core Data because all related objects are already linked by the relationship. This means constructions like where a.id = b.aid and b.id = c.bid aren't needed at all.

Secondly, you generally set the fetch entity for the entity that receives the defining test. In this case, that is c.attr="X"So, you set your fetch entity to C and your predicate should look something like:

NSPredicate *p=[NSPredicate predicateWithFormat:@"attr=%@",xValue];

This will return an array all the C instances that meet the test. Then finding any particular B or A is just a matter of walking the relationship for each C.

If your inverse relationship is to-one e.g A<->>B<->>C, the you just ask each C for the value of the b.a so:

AObject *anA = aCinstance.b.a;

It's important to remember you are not dealing with tables here. You are dealing with an object graph. You set the fetch to a particular entity and then walk the filtered entities' relationships.

这篇关于核心数据:3表连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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