CoreData谓词ANY + AND和to-many关系 [英] CoreData predicate ANY + AND with to-many relationship

查看:261
本文介绍了CoreData谓词ANY + AND和to-many关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的模型中定义的实体:

I have those entities defined in my model:

[Sample]
- results (to many relation with [Result])

[Parameter]
- results (to many relation with [Result])
- compliant (number value)

[Result]
- parameter (relation with [Parameter])
- sample (relation with [Sample])


b $ b

对于一个[Sample]对象MySample,我需要获取所有的[Parameter]对象:

For a [Sample] object "MySample", I need to get all [Parameter] objects that have:

[Result].compliant = 1 AND [Result].sample = MySample

m尝试使用谓词来获取[Parameter]对象,这些谓词将是这些谓词的和:

So, I'm trying to fetch [Parameter] objects using predicate that will be a sum of those predicates:

// returns all parameters that have a result compliant value equal to 1:
ANY results.compliant = 1

// returns all parameters related with my [Sample] object:
ANY results.sample = MySample

两个谓词都按预期工作,但我想一起使用它,因为我需要获取所有参数连接到我的[Sample]对象,其结果符合值等于1。

Both predicates working as expected, but I would like to use it together, because I need to get all parameters connected with my [Sample] object that have result compliant value equal to 1.

我尝试过这样的:

ANY (results.compliant = 1 AND results.sample = MySample)

但它给我无效的谓词异常。

but it gives me "invalid predicate" exceptions.

我也试过使用SUBQUERY,但它给我那些异常:

I have also tried to use SUBQUERY, but it gives me those exception:

Can't have a non-relationship collection element in a subquery

我想注意一下:

ANY results.compliant = 1 AND ANY results.sample = MySample

是一个有效的查询,但不是我要查找的。

is a valid query, but not what I'm looking for.

如何使用单个谓词来解决这个问题,该谓词给出了所有[Parameter]对象具有与[Sample]对象连接的[Result]对象,等于1?

How to solve this problem with a single predicate that gives me all [Parameter] objects that have a [Result] object connected with my [Sample] object and having compliant value equal to 1 ?

UPDATE:

使用NSCompoundPredicate不提供预期结果。我需要这样的:

Using NSCompoundPredicate not giving me expected results. I need something like this:

ANY (results.compliant = 1 AND results.sample = MySample)

并使用复合谓词:

(ANY results.compliant = 1) AND (ANY results.sample = MySample)

复合谓词给我带有合法值等于1的结果的参数,以及具有与MySample相关联的结果的参数,但我需要的是获得具有与MySample连接的结果的参数,并且具有等于1的合规值两个条件都必须遵循单个[Result]对象)。任何想法如何实现?

Compound predicate gives me parameters that have a result with compliant value equal to 1 and parameters that have a result connected with MySample, but what I need is to get parameters that have result that are connected with MySample AND have compliant value equal to 1 (both conditions have to be complied for a single [Result] object). Any ideas how to achieve that?

更新2:

我使用错误的SUBQUERY,解决方案是使用像它应该使用:-)与SUBQUERY我可以得到正确的结果。

It appears that I was using wrong SUBQUERY, the solution is to using like it should be used :-) With SUBQUERY I can get the proper result.

推荐答案

我在手机上输入此内容,因此可能会出现语法错误,但这里需要一个子查询:

I'm typing this on the phone so there might be syntax errors, but this is where you need a subquery:

[NSPredicate predicateWithFormat:@"SUBQUERY(results, $r, $r.compliant = 1 AND $r.sample = %@).@count > 0", mySample]

这篇关于CoreData谓词ANY + AND和to-many关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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