使用多种深层关系构建NSPredicate的更好方法是什么?

Vic*_*tor 21 iphone core-data nspredicate

我有三个实体:EntityA,EntityB和EntityC与多对多关系连接.

请参阅架构了解详情:

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

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

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY EntityB.name like 'SomeName'"];
Run Code Online (Sandbox Code Playgroud)

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

最好的祝福,

胜利者

Vic*_*tor 12

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

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

不幸的是我无法在nsExpression对象上扩展此查询.