NSPredicate过滤器到许多具有子对象属性 [英] NSPredicate filter to-Many with child object property

查看:124
本文介绍了NSPredicate过滤器到许多具有子对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想的是一个简单的问题。我试图过滤一些核心数据,其中我有一个父对象与子对象有一对多的关系,该子对象有一个字符串ID。我想获得所有的父对象,其中没有子对象具有特定的id。

I have what I thought was a simple problem. I am attempting to filter some core data where I have a Parent object which has a to-many relationship with a child object and that child object has a string id. I want to get all the parent objects where no child object has a specific id.

我尝试过!(ANY ... LIKE) / code>以及!(ANY .. ==) NONE 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?更好的解决方案?

Am I missing something with NSPredicate? Is this type of filtering allowed for CoreData? Better solution?

推荐答案

我发现了一个类似的问题,虽然不容易明白。原来的答案是棘手的SUBQUERY。这是我追逐的原因:

I found a similar question although not easily apparent. It turns out the answer is the tricky SUBQUERY. Here is what led me on the chase:

NSPredicate Aggregate Operations with NONE

以及有关SUBQUERY的更开放的说明:

and an more open explanation about SUBQUERY here:

-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];

这篇关于NSPredicate过滤器到许多具有子对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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