我正在使用基于核心数据的时间序列(实体/表),它由以下属性/列组成:
dateTime mc nd re
根据公式result = (mc + nd) / re
,我想计算每一行的结果并在数组中返回结果(日期时间 + 值)。
我尝试使用以下代码在数据库级别执行计算,但在我的结果数组中确实得到了错误的结果(每行多个 dateTime 值,错误的计算结果)。但是,NSExpressionDescription
在 NSLogging 时返回正确的公式。
您对我的代码可能有什么问题有任何想法吗?
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);