我有大量的核心日期对象(大约 10000 个),并且根据配置文件执行代码的时间太长: NSDate *maxExternalChangedDate = [代码 valueForKeyPath:@"@max.externalChangedDate"]; 社区知道找到它的更好方法吗?
NSString *rateSheetID = [rateSheetAndPrefix valueForKey:@"rateSheetID"];
NSFetchRequest *requestCodesForSale = [[NSFetchRequest alloc] init];
[requestCodesForSale setEntity:[NSEntityDescription entityForName:@"CodesvsDestinationsList"
inManagedObjectContext:self.moc]];
[requestCodesForSale setPredicate:[NSPredicate predicateWithFormat:@"(%K.carrier.GUID == %@)",relationshipName,carrierGUID]];
NSError *error = nil;
NSArray *codes = [self.moc executeFetchRequest:requestCodesForSale error:&error];
if (error) NSLog(@"Failed to executeFetchRequest to data store: %@ in function:%@", [error localizedDescription],NSStringFromSelector(_cmd));
NSNumber *count = [codes valueForKeyPath:@"@count.externalChangedDate"];
if (count == 0) {
[requestCodesForSale release];
[pool drain], pool = nil;
return YES;
}
NSDate *maxExternalChangedDate = [codes valueForKeyPath:@"@max.externalChangedDate"];
最佳答案
通过使用 NSFetchRequest
并返回 NSDictionaryResultType
,您可以使用 NSExpressionDescription
来生成 max() 和 min() 等函数的结果。
来自 Apple 的示例代码
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"salary"];
NSExpression *maxSalaryExpression = [NSExpression expressionForFunction:@"max:"
arguments:[NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"maxSalary"];
[expressionDescription setExpression:maxSalaryExpression];
[expressionDescription setExpressionResultType:NSDecimalAttributeType];
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];
查看此文档以获取更多信息。
关于cocoa - 在大型核心数据对象池中查找最大日期的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9194614/