用于在Core Data中执行计算的链式表达式

时间:2013-01-02 12:31:21

标签: objective-c ios sqlite core-data nsexpression

首先:新年快乐: - )

我想做什么

我试图在Core Data中划分两个属性,然后计算这些划分的平均值。属性由密钥路径指定(例如eurusdaud)。

示例: 我有以下数据集:

date         eur   usd    aud
------------------------------
2010-01-01   0.5   1.0    1.5
2010-01-02   0.6   1.1    1.6
2010-01-03   0.4   1.0    1.3

划分两个属性,例如eur / usd以及结果......

divide eur / usd:
------------------
2010-01-01   0.5
2010-01-02   0.54
2010-01-03   0.4

...然后计算这些数字的平均值(0.5 + 0.54 + 0.4)/3 = 0.48

我的代码

由于我希望Core Data直接执行这些计算,因此我创建了以下表达式和获取请求:

NSExpression *fromCurrencyPathExpression = [NSExpression
    expressionForKeyPath:fromCurrency.lowercaseString];
NSExpression *toCurrencyPathExpression   = [NSExpression
    expressionForKeyPath:toCurrency.lowercaseString]; 
NSExpression *divisionExpression = [NSExpression
   expressionForFunction:@"divide:by:"
   arguments:@[fromCurrencyPathExpression,
   toCurrencyPathExpression]];

NSExpression *averageExpression = [NSExpression expressionForFunction:@"average:"
   arguments:@[divisionExpression]];

NSString *expressionName = @"averageRate";
NSExpressionDescription *expressionDescription = 
   [[NSExpressionDescription alloc] init];
expressionDescription.name = expressionName;
expressionDescription.expression = averageExpression;
expressionDescription.expressionResultType= NSDoubleAttributeType;

NSFetchRequest *request = [NSFetchRequest 
   fetchRequestWithEntityName:NSStringFromClass([self class])];
NSPredicate *predicate =
   [NSPredicate predicateWithFormat:@"date >= %@ AND date <= %@",startDate,fiscalPeriod.endDate];

request.predicate = predicate;
request.propertiesToFetch = @[expressionDescription];
request.resultType = NSDictionaryResultType;

NSError *error;
NSArray *results = [context 
    executeFetchRequest:request error:&error];

问题

但是,在运行应用程序时,它会崩溃并显示错误消息:

Unsupported argument to sum : (
    "eur / usd"

我的代码出了什么问题? 如何将两个计算链接起来并直接在Core Data中执行?

谢谢!

1 个答案:

答案 0 :(得分:5)

似乎@average这样的“集合”函数表达式只能用于键路径,但在用作propertiesToFetch时不能与其他常规表达式结合使用。我没有参考,但这是其他人也注意到的一个问题:

所以你可以分两步完成:首先执行一个获取请求,返回一个包含所有分区结果的数组:

NSExpression *fromCurrencyPathExpression = [NSExpression
                                            expressionForKeyPath:@"eur"];
NSExpression *toCurrencyPathExpression   = [NSExpression
                                            expressionForKeyPath:@"usd"];
NSExpression *divisionExpression = [NSExpression
                                    expressionForFunction:@"divide:by:"
                                    arguments:@[fromCurrencyPathExpression,
                                    toCurrencyPathExpression]];

NSString *expressionName = @"ratio";
NSExpressionDescription *expressionDescription =
[[NSExpressionDescription alloc] init];
expressionDescription.name = expressionName;
expressionDescription.expression = divisionExpression;
expressionDescription.expressionResultType= NSDoubleAttributeType;

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Entity"];

request.propertiesToFetch = @[expressionDescription];
request.resultType = NSDictionaryResultType;

NSError *error;
NSArray *ratios = [context executeFetchRequest:request error:&error];

结果是一系列词典:

(lldb) po ratios
(NSArray *) $0 = 0x00000001001170f0 <_PFArray 0x1001170f0>(
{
    ratio = "0.5454545454545454";
},
{
    ratio = "0.4";
},
{
    ratio = "0.5";
}
)

此外,使用“-com.apple.CoreData.SQLDebug 1”选项可以看到分区已在SQLite级别上执行:

sql: SELECT  t0.ZEUR /  t0.ZUSD FROM ZENTITY t0

然后,您可以使用键值编码计算内存中所有比率的平均值:

NSNumber *average = [ratios valueForKeyPath:@"@avg.ratio"];

,结果是

(lldb) po average
(NSNumber *) $0 = 0x000000010012fd60 0.4818181818181818