iOS核心数据条件计数

时间:2014-09-23 13:25:28

标签: ios core-data nsexpression

实际查询 : -

SELECT *,COUNT(case when ZISREAD = 0 then ZISREAD end) FROM ZNOTIFICATION WHERE ZTTL>1411025900  group by zkind,zaction,zname

嗨,我需要使用核心数据COUNT(case when ZISREAD = 0 then ZISREAD end)

转换计数条件NSExpression
 NSExpression *countPathExpression = [NSExpression expressionForKeyPath: @"isRead"];

 NSExpression *countExpression = [NSExpression expressionForFunction: @"count:"
                                                              arguments: [NSArray arrayWithObject:countPathExpression]];

    NSExpressionDescription *countExpressionDescription = [[NSExpressionDescription alloc] init];
    [countExpressionDescription setName: @"count"];
    [countExpressionDescription setExpression: countExpression];
    [countExpressionDescription setExpressionResultType: NSInteger32AttributeType];

我需要帮助人员转换COUNT(case when ZISREAD = 0 then ZISREAD end)

中的NSExpression

1 个答案:

答案 0 :(得分:0)

首先,您可以在获取请求中添加谓词:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"isRead = NO"];

然后,您要在要获取的属性中指定表达式:

fetchRequest.propertiesToFetch = @[countExpression];

不要忘记设置结果类型

fetchRequest.resultType = NSDictionaryResultType;

这应该让你去。请记住,如果要在属性中评估表达式,也可以使用propertiesToGroupBy数组,例如,可以按天分组,这样可以查看每天的计数:)

快乐编码