嵌套子查询无法编译的NSPredicate(核心数据) [英] NSPredicate with nested subqueries failing to compile(core data)

查看:57
本文介绍了嵌套子查询无法编译的NSPredicate(核心数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与CoreDate数据库有关的部分

My relevant part of the CoreDate DB

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

我正在尝试让所有拥有至少一个健身房的用户类别归其所有,而该健身房中的用户拥有活跃的会员资格.我有一个包含所有用户的活动成员uid的数组(每个成员在数据库中都有一个uid)我正在尝试以下无法编译的查询,甚至将其确定为子字符串以确保没有错误:

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?

推荐答案

混合 stringWithFormat predicateWithFormat 容易出错,因为引用和转义规则是不同的.这可能有效(未经测试!):

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

这篇关于嵌套子查询无法编译的NSPredicate(核心数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆