NSPredicate filter to-Many con propiedad de objeto secundario

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.

He tratado !(ANY... LIKE) al igual que !(ANY..==) y NONE with like and == and ALL children.id != otherid

My querying looks like:

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!");

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

preguntado el 27 de noviembre de 13 a las 06:11

posible duplicado de Core Data NSPredicate con una relación de muchos - Summary: That is a Core Data bug and you can use a SUBQUERY as a workaround. -

1 Respuestas

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:

Operaciones agregadas de NSPredicate con NINGUNA

and an more open explanation about SUBQUERY here:


The resulting predicate is:

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

contestado el 23 de mayo de 17 a las 11:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.