如何使用NSPredicate对核心数据关系进行过滤? [英] How can I use NSPredicate to filter on core data relationships?

查看:125
本文介绍了如何使用NSPredicate对核心数据关系进行过滤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类型为obj的核心数据对象,它具有属性propertyA,并且与具有两个属性propertyB和propertyC的类型为sub的对象具有一对多关系。

Say I have core data objects of type "obj" that has a property "propertyA" and a one-to-many relationship with an object of type "sub" that has two properties, "propertyB" and "propertyC".

我要获取所有属性A等于一个值的obj和一个带有propertyB和propertyC set的子obj。

I want to fetch all the objs that have propertyA equal to a value and a sub obj with propertyB and propertyC set.

如果只是propertyA和propertyB,我会做

If it was just propertyA and propertyB, I would do

[NSPredicate predicateWithFormat:@"ANY sub.propertyB = %@ AND propertyA == %@", ...];

问题是我不知道如何添加第二个属性。我只想要有至少一个sub的两个属性为true的obj。我试过以下,但它不工作:

The problem is that I can't figure out how to add in the second property. I want only the objs that have at least one sub that has the two properties true. I've tried the following, but it doesn't work:

[NSPredicate predicateWithFormat:@"ANY (sub.propertyB = %@ AND sub.propertyC) AND propertyA == %@", ...];

我试过没有ANY,但也不工作。

I've tried it without the ANY but that doesn't work either. How can I do this?

推荐答案

由于与子对象有一对多的关系, subs 属性 obj 返回一个集合而不是单个对象。要查询集合,您需要使用SUBQUERY。

Since you have a to-many relationship with the sub object, the subs property of obj returns a set instead of a single object. To query the set, you need to use a SUBQUERY.

子查询的格式如下:

SUBQUERY(collection, $individualCollectionItem, expression-with-collection-item)

在这种情况下,你会想要

in this case you would want something like

SUBQUERY(subs,$s,$s.propertyB==%@) AND SUBQUERY(subs,$s,$s.propertyC!=NULL)

这篇关于如何使用NSPredicate对核心数据关系进行过滤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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