Query based on relationship, using NSPredicate on Core Data

1

I have a one-to-many relationship between the Pai and Filho entities, where a Pai can have any or N Filho . Using Core Data , how do I search all Filho , basing my search for a Pai .

For example: I have a Pai that has Id 1. I need to search all Filho that has Pai with Id 1.

    
asked by anonymous 07.11.2014 / 18:58

1 answer

2

Just use the property names of both the entity and the relationship in the NSPredicate condition to make the request.

Swift :

let request = NSFetchRequest(entityName: "Filho")
request.predicate = NSPredicate(format: "RelacionamentoPai.Id == %d", 1)

Objective-C :

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Filho"];
[request setPredicate:[NSPredicate predicateWithFormat:@"RelacionamentoPai.Id == %d", 1]];
    
07.11.2014 / 20:34