NSPredicate for relationships 2 to many

I have a problem with the NSPredicate syntax. I have 3 objects: A, B and C. A has a many-to-many relationship to B. B has a many-to-many relationship to C. I want to get all managed objects in A that have at least one relationship with an object in B, which in turn has a connection with at least one object in C.

I tried several variations of SUBQUERY and ANY, but I obviously do not use the correct syntax.

I tried:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *ent = [NSEntityDescription entityForName:@"A" inManagedObjectContext:self.managedObjectContext];
fetchRequest.entity = ent;
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(SUBQUERY(B, $b, $b.c.@count > 0).@count > 0)"];

This led to:

2014-06-30 09: 07: 12.143 My application [86086: 60b] * The application terminated due to the uncaught exception "NSInvalidArgumentException", reason: "The path to the key containing the KVC aggregate, where it should not be; failed to process $ b.jobs. @count '

Then:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"ANY b.c.@count > 0"];

Led to:

2014-06-30 09: 10: 14.403 [86186: 60b] * - "NSInvalidArgumentException", : " : (bc)"

:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(SUBQUERY(B, $b, $b.c.@count > 0))"];

:

2014-06-30 09: 17: 25.883 [86218: 60b] * - "NSInvalidArgumentException", : " " (SUBQUERY (B, $b, $bc @count > 0)) "

:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(SUBQUERY(B, $b, $b.c).@count > 0)"];

:

2014-06-30 09: 18: 30.202 [86272: 60b] * - "NSInvalidArgumentException", : " " (SUBQUERY (B, $b, $bc). @count > 0) "

- ? ...

+4
1

:

[NSPredicate predicateWithFormat:@"SUBQUERY(bs, $b, ANY $b.cs != NULL).@count > 0"]

"bs" - A: B, "cs" - B C.

+1

Source: https://habr.com/ru/post/1546565/


All Articles