CoreData谓词ANY + AND和to

编程入门 行业动态 更新时间:2024-10-26 21:24:18
本文介绍了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]pliant = 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 resultspliant = 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 (resultspliant = 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 resultspliant = 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 (resultspliant = 1 AND results.sample = MySample)

并使用复合谓词:

(ANY resultspliant = 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, $rpliant = 1 AND $r.sample = %@).@count > 0", mySample]

更多推荐

CoreData谓词ANY + AND和to

本文发布于:2023-11-12 07:11:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580846.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:谓词   CoreData

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!