create a Compound Predicate in coreData xcode iphone

core-data, ios, iphone, nspredicate

Solution

You can use a compound predicate:

NSPredicate *p1 = [NSPredicate predicateWithFormat:@"studentsToClass.className = %@", @"5th"];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"studentsToExamRecord.result = %@", @"Pass"];
NSPredicate *p = [NSCompoundPredicate andPredicateWithSubpredicates: @[p1, p2]];

Or you simply combine the tests with "AND":

NSPredicate *p = [NSPredicate predicateWithFormat:@"studentsToClass.className = %@ AND studentsToExamRecord.result = %@",
      @"5th", @"Pass"];

Note that the argument list of `predicateWithFormat` is not `nil`-terminated. The number of arguments is determined by the number of format specifiers in the format string.

Problem

HI i am working on the core data with 3 entities (Class,Students,ExamRecord) and their relations area as : ``` Class<------>> Students <------> ExamRecord ``` I created a predicate for fetching list of students for class 5th. ``` NSString * fmt2 = @"studentsToClass.className=%@"; NSPredicate * p2 = [NSPredicate predicateWithFormat:fmt2,@"5th",nil]; ``` with this i am getting all students of class 5th Now i also want to apply another filter on the Students fetched. Fetch students whose Exam Record "result" is "Pass".result is an attribute for student in ExamResult entity How can i make use of Compound predicate in this ? Please correct me if i am wrong Any help will be appreciated Thanks

Original source