NSPredicate在嵌套对象/ NSSet上以在NSFetchRequest过滤结果 [英] NSPredicate on nested object / NSSet to filter the result during NSFetchRequest

查看:325
本文介绍了NSPredicate在嵌套对象/ NSSet上以在NSFetchRequest过滤结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个简单的谓词,它返回所有具有mode = 0和组中的注册模式的组= 0



访问嵌套对象属性的谓词。
不知何故一个谓词:

  [NSPredicate predicateWithFormat:@mode = 0 AND enrollments.Enrollment.mode = 0 ] 

上述谓词是错误的,显然不起作用。



已编辑:



未成功。

  [NSPredicate predicateWithFormat:@mode = 0 AND ALL(SUBQUERY(enrollments,$ varEnrollment,$ varEnrollment.mode = 0))] 



我需要结果包含所有活动组(group.mode = 0)和所有活跃的登记者(enrolles.mode = 0)
但对我来说这个谓词不起作用。



解决方案

从您的问题和

$

  [NSPredicate predicateWithFormat:@mode = 0 AND(ALL enrollments.mode = 0)] 

UPDATE b
$ b

似乎ALL聚合不起作用。它引发

 'NSInvalidArgumentException',原因:'不支持的谓词(null)'

为@yunas注意到。以前也注意到这一点,例如





另一方面,ANY聚合工作正常。因此,作为替代方法,可以用等效的任何表达式替换ALL:

  [NSPredicate predicateWithFormat: @mode = 0 AND NOT(ANY enrollments.mode!= 0)]; 

这实际上是有效的(我测试了它)!



UPDATE 2



在讨论期间,yunas希望显示一个表格, mode = 0,并在表格行中显示该组的所有注册(mode = 0)。



第一个解决方案>首先使用上面给出的方法查找所有允许的组。对于每一行( cellForRowAtIndexPath:)过滤该组的注册并绘制单元格。



第二个解决方案:提取所有注册并按组进行排序。这只需要一个获取请求,但是结果不适合作为表视图数据源。抓取请求如下所示:

  NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@Enrollment]; 
NSPredicate * predicate = [NSPredicate predicateWithFormat:@mode = 0 AND group.mode = 0];
request.predicate = predicate;
NSSortDescriptor * sort1 = [NSSortDescriptor sortDescriptorWithKey:@group.nameascending:YES];
NSSortDescriptor * sort2 = [NSSortDescriptor sortDescriptorWithKey:@shareascending:YES];
request.sortDescriptors = [NSArray arraysWithObjects:sort1,sort2,nil];


I want a simple predicate that returns me all the groups which have mode = 0 and the mode of the enrollments in the group = 0

To be precise i need a predicate to access the nested object properties. Somehow a predicate like:

[NSPredicate predicateWithFormat:@"mode = 0 AND enrollments.Enrollment.mode = 0"]

the above predicate is wrong and obviously doesn't work.

EDITED:

I have given a go to the following predicate too but been unsuccessful.

[NSPredicate predicateWithFormat:@"mode = 0 AND ALL ( SUBQUERY(enrollments,$varEnrollment,$varEnrollment.mode = 0))"]

I need result which contains all groups that are active (group.mode = 0) AND with all enrollee's that are active (enrolles.mode = 0) but for me this predicate doesn't work.

解决方案

From your question and the comments I guess that you want

[NSPredicate predicateWithFormat:@"mode = 0 AND (ALL enrollments.mode = 0)"]

UPDATE

It seems that the ALL aggregate does not work. It throws

'NSInvalidArgumentException', reason: 'Unsupported predicate (null)'

as @yunas noticed. This has also been noticed previously, e.g.

On the other hand, the ANY aggregate works just fine. So as a WORKAROUND, one can replace ALL with an equivalent ANY expression:

[NSPredicate predicateWithFormat:@"mode = 0 AND NOT(ANY enrollments.mode != 0)"];

And this actually works (I have tested it)!

UPDATE 2

During the discussion it has become clear that yunas wants to display a table with one row for each group with mode=0, and within the table row display all enrollments of that group with mode=0.

First solution (probably the better one): Find all admissible groups first with the method given above. For each row (in cellForRowAtIndexPath:) filter the enrollments for that group and draw the cell.

Second solution: Fetch all enrollments and sort them by group. This requires only one fetch request, but the result is not so suitable as table view data source. The fetch request would look like this:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Enrollment"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"mode = 0 AND group.mode = 0"];
request.predicate = predicate;
NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"group.name" ascending:YES];
NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:@"share" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObjects:sort1, sort2, nil];

这篇关于NSPredicate在嵌套对象/ NSSet上以在NSFetchRequest过滤结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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