【问题标题】:Core data many to many relationship query核心数据多对多关系查询
【发布时间】:2013-03-07 12:03:59
【问题描述】:

假设有两个实体:EntityA 和 EntityB,通过多对多关系连接。假设关系属性分别是relatedEntityBs 和relatedEntityAs。我有一个 NSArray 的 EntityB 实例。我想找到与 nsarray 中的所有实例相关的所有 EntityA 实例。也就是说,如果数组是 [entityB1,.....,entityBn]。查找与 entityB1、wntityB2 直到 entityBn 相关的所有 EntityA 实例。这种谓词怎么写?

【问题讨论】:

标签: ios objective-c core-data nspredicate


【解决方案1】:

这个谓词应该这样做:

NSMutablArray *predicateArray = [NSMutableArray array];
for (int i=0;i<entityBArray.count;i++){
     NSPredicate *subPredicate = [NSPredicate predicateWithFormat:@"ANY relatedEntityBs contains %@",[entityBArray objectAtIndex:i];
     [predicateArray addObject:subPredicate] ;
} 
NSCompoundPredicate *daddyPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicateArray];

基本上每个子谓词检查relatedEntityB 是否包含数组的单个对象,最后daddyPredicate 将它们全部用OR 组合起来。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 2011-02-18
    • 2016-09-08
    • 1970-01-01
    • 2014-08-13
    相关资源
    最近更新 更多