我有一个 fetchRequest 可以正确返回特定键路径的最大值。设置看起来像这样:
NSExpression *keyExpression = [NSExpression expressionForKeyPath:@"distance"];
NSExpression *functionExpression = [NSExpression expressionForFunction:@"max:" arguments:@[keyExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:keyPath];
[expressionDescription setExpression:functionExpression];
[expressionDescription setExpressionResultType:NSInteger32AttributeType];
问题是我还需要返回与上述 NSExpressionDescription 匹配的对象上另一个属性的值。换句话说,我想要为 managedObject 返回的时间戳属性的值,从中返回 max: 值。如果我这样设置获取请求:
NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"record"];
request.resultType = NSDictionaryResultType;
request.propertiesToFetch = @[expressionDescription, @"timestamp"];
request.propertiesToGroupBy = @[@"timestamp"];
它为每个具有时间戳的记录返回一个字典,而不是只为匹配最大值的记录返回一个字典。知道如何获得我想要的结果吗?