在 Core Data 中执行计算的链式表达式 [英] Chained expressions to perform calculations in Core Data
问题描述
首先:新年快乐:-)
我试图在 Core Data 中划分两个属性,然后计算这些划分的平均值.属性由密钥路径指定(例如 eur
、usd
、aud
).
I am trying to divide two attributes in Core Data and then calculate the average of these divisions. The attributes are specified by a key path (e.g. eur
, usd
, aud
).
示例:我有以下数据集:
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
划分两个属性,例如欧元/美元与以下结果...
Divide two attributes, e.g. eur / usd with the follwowing results...
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
... then calculate the average of these numbers (0.5 + 0.54 + 0.4)/3 = 0.48
因为我想让 Core Data 直接执行这些计算,所以我创建了以下表达式并获取请求:
Since I would like to have these calculations performed directly by Core Data, I created the following expressions and fetch request:
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];
问题
但是,在运行应用程序时,它崩溃并显示错误消息:
The problem
However, when running the app, it crashes with the error message:
Unsupported argument to sum : (
"eur / usd"
我的代码有什么问题?如何链接这两个计算并让它们直接在 Core Data 中执行?
What is wrong with my code? How can I chain the two calculations and have them performed directly in Core Data?
谢谢!
推荐答案
看来像 @average
这样的集合"函数表达式只能与关键路径一起使用,而不能与其他通用组合使用表达式,当用作 propertiesToFetch
时.我没有这方面的参考,但其他人也注意到了这个问题:
It seems that "collection" function expressions like @average
can only be used with key paths, but not in combination with other general expressions, when used as propertiesToFetch
. I do not have a reference for that, but it is a problem that others have noticed also:
因此您可以分两步进行:首先执行一个获取请求,该请求返回一个包含所有除法结果的数组:
So you could proceed in two steps: First execute a fetch request that returns an array with the results of all divisions:
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 级别执行:
Also, with the "-com.apple.CoreData.SQLDebug 1" option one can see that the divisions are already executed on the SQLite level:
sql: SELECT t0.ZEUR / t0.ZUSD FROM ZENTITY t0
然后您可以使用键值编码计算内存中所有比率的平均值:
Then you can compute the average of all ratios in memory, using Key-Value Coding:
NSNumber *average = [ratios valueForKeyPath:@"@avg.ratio"];
结果是
(lldb) po average
(NSNumber *) $0 = 0x000000010012fd60 0.4818181818181818
这篇关于在 Core Data 中执行计算的链式表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!