I m sure I m guilty of trying to apply SQL logic to Core Data, but even after reading the Apple docs I m still not sure of the correct approach.
在核心数据实体中总结两个日期财产差异的最佳战略是什么? 我的“南Hours”按“appt”类别计算,但没有在Xcdatamodel计算。 下面的法典没有“在实体中发现的关键”错误。 许多人感谢任何建议。
from appt class
- (float)numHours {
float hours = ([self.endTime timeIntervalSinceDate:self.startTime] - [self.durationOfBreak intValue]) / 3600.00;
return hours;
}
attempt at fetching sum of "numHours" property
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"appt" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
NSPredicate *datePredicate = [NSPredicate predicateWithFormat:@"date >= %@ && date < %@", self.startDate, self.endDate];
[request setPredicate:datePredicate];
[request setResultType:NSDictionaryResultType];
NSExpression *numHoursPath = [NSExpression expressionForKeyPath:@"numHours"];
NSExpression *hoursSum = [NSExpression expressionForFunction:@"sum:"
arguments:[NSArray arrayWithObject:numHoursPath]];
NSExpressionDescription *debitExpressionDescription = [[NSExpressionDescription alloc] init];
[debitExpressionDescription setName:@"totalhours"];
[debitExpressionDescription setExpression:hoursSum];
[debitExpressionDescription setExpressionResultType:NSDecimalAttributeType];
[request setPropertiesToFetch:[NSArray arrayWithObject:debitExpressionDescription]];
[debitExpressionDescription release];
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];