我是 Core Data 的新手,我正在尝试获取具有最大时间戳值的记录。在以下代码中,我得到了最大的 TimeStamp 值,但我希望该记录具有最大的时间戳值。
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"EnergyCumulative" inManagedObjectContext:context];
NSFetchRequest *fetchRequest = [[ NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
NSExpression *keyPathExpression = [NSExpression
expressionForKeyPath:@"TimeStamp"];
NSExpression *ex = [NSExpression expressionForFunction:@"max:"
arguments:[NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"AvgReading"];
[expressionDescription setExpression:ex];
[expressionDescription setExpressionResultType:NSStringAttributeType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *tripArray = [context executeFetchRequest:fetchRequest error:&error];
NSLog(@"%@",tripArray);
在 NSLog 我得到( { AvgReading = 1342704600000; } )
前任。我有实体EnergyCumulative,因为我有属性Reading & TimeStamp,所以我想要具有最大时间戳的读数