GROUP BY与SUM()使用Core Data [英] GROUP BY with SUM() using Core Data

查看:268
本文介绍了GROUP BY与SUM()使用Core Data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了类似的问题,但没有一个完整的例子说明如何完成。



我想翻译的SQL查询是: p>

  SELECT date,SUM(amount)FROM Table GROUP BY date; 



我需要帮助调试以下代码(目前fetchRequest返回nil):

  entity = [NSEntityDescription entityForName:@TableinManagedObjectContext:self.managedObjectContext]; 
[fetchRequest setEntity:entity];

NSExpressionDescription * ex = [[NSExpressionDescription alloc] init];
[ex setExpression:[NSExpression expressionWithFormat:@@ sum.amount]];
[ex setExpressionResultType:NSDecimalAttributeType];

[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@date,ex,nil]];
[fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObject:@date]];
[fetchRequest setResultType:NSDictionaryResultType];

[self.managedObjectContext executeFetchRequest:fetchRequest error:& error];

这是错误:

  2012-09-28 13:58:46.319 App [12205:c07] ***由于未捕获异常NSInvalidArgumentException终止应用程序,原因:'***  -  [__ NSArrayM insertObject:atIndex: ]:object can not be nil'


解决方案

  [ex setName:@somename]; 



在执行fetchRequest之前,文章:



http://iphonedevelopment.blogspot。 com / 2010/11 / nsexpression.html


I came across similar questions but none of them had a complete example of how to accomplish it.

The SQL query I am trying to translate is this:

SELECT date, SUM(amount) FROM Table GROUP BY date;

I need help debugging the following piece code (currently the fetchRequest returns nil):

entity = [NSEntityDescription entityForName:@"Table" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

NSExpressionDescription* ex = [[NSExpressionDescription alloc] init];
[ex setExpression:[NSExpression expressionWithFormat:@"@sum.amount"]];
[ex setExpressionResultType:NSDecimalAttributeType];

[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"date", ex, nil]];
[fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObject:@"date"]];
[fetchRequest setResultType:NSDictionaryResultType ];

[self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

This is the error:

2012-09-28 13:58:46.319 App[12205:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

解决方案

The code above needs

[ex setName:@"somename"];

before executing the fetchRequest.

Useful article here:

http://iphonedevelopment.blogspot.com/2010/11/nsexpression.html

这篇关于GROUP BY与SUM()使用Core Data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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