【问题标题】:NSPredicate filter to-Many with child object propertyNSPredicate 过滤到具有子对象属性的多
【发布时间】:2013-12-12 16:14:11
【问题描述】:

我有一个我认为很简单的问题。我正在尝试过滤一些核心数据,其中我有一个父对象,该对象与子对象具有一对多关系,并且该子对象具有字符串 id。我想获取没有子对象具有特定 id 的所有父对象。

我尝试了!(ANY... LIKE) 以及!(ANY..==)NONE 与like 和== 和ALL children.id != otherid

我的查询看起来像:

NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Parent"];

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"NONE children.id LIKE %@",otherID];
[fetchRequest setPredicate: predicate];
NSError* error;
NSArray* allParents = [[DataManager context] executeFetchRequest:fetchRequest error:&error];
//sanity check the predicate
for (Parent* p in allParents) {
    for (Child* c in p.children) {
        if([c.id isEqualToString:otherID]){
            NSLog(@"PREDICATE FAIL!");
        }
    }
}

我是否缺少 NSPredicate 的某些内容? CoreData 是否允许这种类型的过滤?更好的解决方案?

【问题讨论】:

标签: ios objective-c core-data nspredicate


【解决方案1】:

我发现了一个类似的问题,虽然不是很明显。事实证明,答案是棘手的 SUBQUERY。以下是引导我追逐的原因:

NSPredicate Aggregate Operations with NONE

还有一个关于 SUBQUERY 的更开放的解释:

http://funwithobjc.tumblr.com/post/2726166818/what-the-heck-is-subquery

结果谓词是:

//in children get a $child and group all the $child objects together
//where the ids match, if that groups count is 0 we know 
//the parent has no child with that id
[NSPredicate predicateWithFormat:
   @"SUBQUERY(children, $child, $child.id == %@).@count == 0",objectId];

【讨论】:

    猜你喜欢
    • 2010-12-30
    • 1970-01-01
    • 2017-09-20
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 2013-11-23
    • 2021-07-07
    相关资源
    最近更新 更多