CoreData predicate ANY + AND with to-many relationship

。_饼干妹妹 提交于 2020-01-06 04:07:10

问题


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

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

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

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

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.

I have tried something like that:

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

but it gives me "invalid predicate" exceptions.

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

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

I would like to notice, that query:

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

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

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:

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

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

and using compound predicate works like that:

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

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?

UPDATE 2:

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.


回答1:


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]



回答2:


I would recommend using a NSCompoundPredicate.

Create your predicates and add them to an array then assign them as a NSCompoundPredicate like so.

NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];


来源:https://stackoverflow.com/questions/17026765/coredata-predicate-any-and-with-to-many-relationship

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