链接表达式以在Core Data中执行计算 [英] Chained expressions to perform calculations in Core Data
问题描述
首先:新年快乐: - )
我想做什么
我试图在Core Data中划分两个属性,然后计算这些分割的平均值。属性由关键路径指定(例如 eur
, usd
, aud $例如:
我有以下数据集:
b $ b
日期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 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
我的代码
由于我想直接由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];
问题
该应用程序崩溃时出现错误消息:
不支持的参数sum:(
eur / usd
我的代码有什么问题?
如何链接两个计算,直接在Core Data中?
谢谢!
@average
之类的集合函数表达式只能用于键路径,但不能与其他一般表达式结合使用 propertiesToFetch
。我没有这个参考,但是这是一个问题,其他人也注意到:
- a href =http://stackoverflow.com/questions/13879025/fetch-aggregate-data-from-nsmanagedobject-using-another-expression-as-argument-t>使用另一个表达式作为参数从NSManagedObject提取聚合数据sum:expression
- 使用CoreData执行乘法(聚合):如何?
因此,您可以分两步执行:首先执行fetch请求返回一个包含所有分区结果的数组:
NSExpression * fromCurrencyPathExpression = [NSExpression
expressionForKeyPath:@欧元];
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
First of all: Happy new year :-)
What I am trying to do
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
).
Example:
I have the following data set:
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
... then calculate the average of these numbers (0.5 + 0.54 + 0.4)/3 = 0.48
My code
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"
What is wrong with my code?
How can I chain the two calculations and have them performed directly in Core Data?
Thank you!
解决方案 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:
- Fetch aggregate data from NSManagedObject using another expression as argument to sum: expression
- Performing multiplication (aggregation) with CoreData: how to?
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];
The result is an array of dictionaries:
(lldb) po ratios
(NSArray *) $0 = 0x00000001001170f0 <_PFArray 0x1001170f0>(
{
ratio = "0.5454545454545454";
},
{
ratio = "0.4";
},
{
ratio = "0.5";
}
)
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"];
and the result is
(lldb) po average
(NSNumber *) $0 = 0x000000010012fd60 0.4818181818181818
这篇关于链接表达式以在Core Data中执行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!