使用多对多深度关系构建 NSPredicate 的更好方法是什么? [英] What's better way to build NSPredicate with to-many deep relationships?

查看:21
本文介绍了使用多对多深度关系构建 NSPredicate 的更好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个实体:EntityA、EntityB 和 EntityC,它们以多对多的关系相连.

I have three entities: EntityA, EntityB and EntityC connected with to-many relationships.

有关详细信息,请参阅架构:

See schema for details:

替代文字 http://img706.imageshack.us/img706/9974/screenshot20091220at124.png

为了获取依赖于 EntityB.name 的 EntityA 的所有实例,我使用这样的谓词:

For getting all instance of EntityA which depend from EntityB.name I use the predicate like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY EntityB.name like 'SomeName'"];

获取依赖于 EntityC.name 的所有 EntityA 实例的谓词应该是什么?我尝试了像 @"ANY EntityB.entitiesC.name like 'SomeName'" 这样的查询,但得到异常 "multiple to-many keys not allowed here".

What should be predicate for getting all instance of EntityA which depend from EntityC.name? I tried query like @"ANY EntityB.entitiesC.name like 'SomeName'" but get exception "multiple to-many keys not allowed here".

最好的问候,

维克多

推荐答案

我的最终解决方案是使用 SUBQUERY.

My final solution is to use SUBQUERY.

NSPredicate *p = [NSpredicate predicateWithFormat:@"(name like %@) AND (0 != SUBQUERY(entitiesB, $x, (0 != SUBQUERY($x.entitiesC, $y, $y.name like %@).@count)).@count)", nameA, nameC];

很遗憾,我无法在 nsExpression 对象上扩展此查询.

Unfortunately I was unable to expand this query on nsExpression objects.

这篇关于使用多对多深度关系构建 NSPredicate 的更好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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