I am new to Core Data,I am trying to fetch a record which has maximum Timestamp value. In Following code I am getting the maximum TimeStamp value but I want that record that has maximum timestamp value.
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);
In NSLog I am getting ( { AvgReading = 1342704600000; } )
Ex. I have Entity EnergyCumulative in that I have attribute Reading & TimeStamp, so I want that reading that has maximum timestamp