NSPredicate,获得具有一对多关系子集的结果 [英] NSPredicate, get results with a subset of one-to-many relationship
问题描述
我正在使用Core Data和 NSFetchedResultsController
。
我的数据模型如下所示:
产品
与一对多的关系称为 dataLines
。
dataLine
实体具有属性名 theWeek
。
我要获取所有产品
,其中 dataLines.theWeek == someValue
。这很容易用子查询完成。但这返回所有的dataLines。是否可以创建返回 Product
的 NSPredicate
和一个子集,如果dataLines只与 dataLines == someValue
?
使用SUBQUERY
[NSPredicate predicateWithFormat:@SUBQUERY dataLines,$ x,$ x.theWeek ==%@)。@ count> 0),[NSNumber numberWithInt:18]];
或ANY修饰符
[NSPredicate predicateWithFormat:@ANY dataLines.theWeek ==%@,[NSNumber numberWithInt:18]];
如果您需要检查多个值,您还可以执行以下操作:
[NSPredicate predicateWithFormat:@SUBQUERY(dataLines,$ x,$ x.theWeek ==%@或$ x.theWeek ==%@)。 @count> 0),[NSNumber numberWithInt:18],[NSNumber numberWithInt:19]];
同样适用于 ANY
。 任何...或任何...
。
也许如果你分享一些代码,我们可以帮助你。 / p>
PS我想你不使用标量值和 theWeek
是一个数字。
希望它有帮助。 >
I'mm working around with Core Data and NSFetchedResultsController
.
My Data Model looks like this:
Product
with one-to-many relationship called dataLines
.
The dataLine
entity has a property name theWeek
.
I want to fetch all Product
where dataLines.theWeek == someValue
. This is easily done with a subquery. But this returns all dataLines. Is it possible to create a NSPredicate
that returns the Product
and a subset if dataLines only with the dataLines == someValue
?
What you want to achieve could be reached in two ways:
using a SUBQUERY
[NSPredicate predicateWithFormat:@"SUBQUERY(dataLines, $x, $x.theWeek == %@).@count > 0)", [NSNumber numberWithInt:18]];
or the ANY modifier
[NSPredicate predicateWithFormat:@"ANY dataLines.theWeek == %@", [NSNumber numberWithInt:18]];
You can do also the following if you need to check against multiple values:
[NSPredicate predicateWithFormat:@"SUBQUERY(dataLines, $x, $x.theWeek == %@ or $x.theWeek == %@).@count > 0)", [NSNumber numberWithInt:18], [NSNumber numberWithInt:19]];
The same can be applied to ANY
modifier. ANY ... OR ANY ...
.
Maybe if you share some code we could help you.
P.S. I suppose you don't use scalar values and theWeek
is a number.
Hope it helps.
这篇关于NSPredicate,获得具有一对多关系子集的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!