NSPredicate with nested subqueries failing to compile(core data)

青春壹個敷衍的年華 提交于 2019-12-11 22:49:13

问题


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?


回答1:


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];


来源:https://stackoverflow.com/questions/22863505/nspredicate-with-nested-subqueries-failing-to-compilecore-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!