Objective C - one-many relationships data retrival from core data
core-data, ios, objective-c
Solution
this looks like a case for: `SUBQUERY`
NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([Author class])];
[NSPredicate predicateWithFormat:@"(SUBQUERY(books, $cd, SUBQUERY($cd.chapters, $m, $m.readDate == NULL).@count != 0).@count != 0"];
[fetch setPredicate:uidCondition];
not sure if it was == nil or NULL what to check for in dataBase. I'm only checking for values so can't tell this at the moment
Problem
I have a core data model (has made a simplifie to make it easier to explain what I want to do). An author can published many books and a book can have many chapters. In the example I want to retreive all the Autors that has a chapter in a book where readDate is null. ``` - (NSArray *)incompleteChaptersInManagedObjectContext:(NSManagedObjectContext *)context { NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([Author class])]; NSPredicate* uidCondition = [NSPredicate predicateWithFormat:@"books.chapters.readDate!=nil"]; [fetch setPredicate:uidCondition]; NSArray* resultArray = [context executeFetchRequest:fetch error:&error]; .. .. ``` This does not work and I am struggling finding out how this can be done. At the moment I am declare an author mutablearray. - get all authors - loop through all their books - loop through all the chapters - if readDate is null add it to the author array This works but I want to solve this without the nested loops.