Question

My relevant part of the CoreDate DB

Category<-->>Subcategory<-->>Gym<<-->>Membership

I am trying to get all the Categories who have at lease one gym where the user has an active membership. I have an array with all the user's active membership uid's(every membership has a uid in the DB) I am trying the following query which fails to compile, I even decided it into substrings just to make sure no errors are made:

NSString *shopsSubquery = [NSString stringWithFormat:@"(SUBQUERY($y.programs,$z, $z.uid IN %@).@count > 0)",activeProgramsUIDsArray];
NSString *subcategorySubquery = [NSString stringWithFormat:@"(SUBQUERY($x.gyms,$y, %@).@count > 0)",shopsSubquery] ;
predicate = [NSPredicate predicateWithFormat:@"(SUBQUERY(subcategories, $x, %@).@count > 0)", subcategorySubquery]; 

The compiler is being very unhelpful as to the source of the problem. I have been rewriting this predicate over 10 times, can anything figure out what am I missing?

Was it helpful?

Solution

Mixing stringWithFormat and predicateWithFormat is error-prone because the quoting and escaping rules are different. This might work (untested!):

[NSPredicate predicateWithFormat:@"SUBQUERY(subcategories, $x, SUBQUERY($x.gyms, $y, ANY $y.programs.uid IN %@).@count > 0).@count > 0", activeProgramsUIDsArray];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top