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

编程入门 行业动态 更新时间:2024-10-23 11:30:23
本文介绍了在 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:

  • 使用另一个从 NSManagedObject 获取聚合数据表达式作为 sum 的参数:表达式
  • 使用 CoreData 执行乘法(聚合):如何?

因此您可以分两步进行:首先执行一个获取请求,该请求返回一个包含所有除法结果的数组:

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 中执行计算的链式表达式

本文发布于:2023-11-28 15:35:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642984.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链式   表达式   Core   Data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!