简体   繁体   English

对核心数据属性/列执行基本计算

[英]Performing basic calculations on Core Data attributes / columns

I am using Core Data based time series (entity/table) which consists of the following attributes / columns: 我正在使用基于核心数据的时间序列(实体/表),该时间序列由以下属性/列组成:

dateTime mc nd re

Based on the formula result = (mc + nd) / re I would like to calculate the result for each row and return the results (dateTime + value) in an array. 基于公式result = (mc + nd) / re我想计算每一行的结果,并在数组中返回结果(dateTime +值)。

I tried the following code to perform the calculations on the database level but do get wrong results in my result array (multiple dateTime values per row, wrong calculation results). 我尝试使用以下代码在数据库级别执行计算,但是在我的结果数组中确实得到了错误的结果(每行多个dateTime值,错误的计算结果)。 However, the NSExpressionDescription returns the correct formula when NSLogging it. 但是, NSExpressionDescriptionNSExpressionDescription返回正确的公式。

Do you have any idea on what might be wrong with my code? 您对我的代码可能有什么问题有任何想法吗?

NSString *divisorKeyPath = @"revenue"
NSExpression *marketCapExpression = [NSExpression expressionForKeyPath:@"mc"];
NSExpression *netDebtExpression   = [NSExpression expressionForKeyPath:@"nd"];
NSExpression *divisorExpression   = [NSExpression expressionForKeyPath:
    divisorKeyPath];
NSExpression *evExpression        = [NSExpression expressionForFunction:
    @"add:to:"  
    arguments:@[marketCapExpression,netDebtExpression]];
NSExpression *divisionExpression  = [NSExpression expressionForFunction:
    @"divide:by:"
    arguments:@[evExpression, divisorExpression]];
NSString *divisionExpressionName = @"division";
NSExpressionDescription *divisionDescription  = [[NSExpressionDescription alloc] init];
divisionDescription.name                      = divisionExpressionName;
divisionDescription.expression                = divisionExpression;
divisionDescription.expressionResultType      = NSDoubleAttributeType;

NSFetchRequest *divisionRequest = [NSFetchRequest fetchRequestWithEntityName:
   NSStringFromClass([SharePrice class])];
divisionRequest.propertiesToFetch = @[@"dateTime",divisionDescription];
divisionRequest.resultType = NSDictionaryResultType;
NSSortDescriptor *sortDescriptor = [NSSortDescriptor 
   sortDescriptorWithKey:@"dateTime" ascending:YES];
divisionRequest.sortDescriptors = @[sortDescriptor];

NSError *error;
NSArray *divisionResults = [self.managedDocument.managedObjectContext
   executeFetchRequest:divisionRequest error:&error];
NSLog(@"results: %@",divisionResults);

This seems to be a bug in Core Data. 这似乎是核心数据中的错误。 If you set the launch argument "-com.apple.CoreData.SQLDebug 3" then you can see that the expression description is translated to the SQLite query 如果将启动参数设置为“ -com.apple.CoreData.SQLDebug 3”,则可以看到表达式描述已转换为SQLite查询

SELECT t0.ZDATETIME,  t0.ZMARKETCAP +  t0.ZNETDEBT /  t0.ZREVENUE FROM ZENTITY t0 ORDER BY t0.ZDATETIME

and not 并不是

SELECT t0.ZDATETIME,  (t0.ZMARKETCAP +  t0.ZNETDEBT) /  t0.ZREVENUE FROM ZENTITY t0 ORDER BY t0.ZDATETIME

as it should be. 应该是。

The divisionExpression itself is correct. divisionExpression本身是正确的。 If you apply it to a single sharePrice object of your entity, using 如果将其应用于实体的单个sharePrice对象,请使用

NSNumber *x = [expression expressionValueWithObject:sharePrice context: nil];

it returns the expected result. 它返回预期的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM