Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

an NSPredicate for selecting a row with maximum value by group

I'm using Core Data to store entities in the form

TrackerEntry

  • username
  • timestamp

I want to select the latest record for each user. Using sql it would be something like

SELECT MAX(timestamp) FROM Log GROUP BY username

Is there anyway to create an NSPredicate to do this?

like image 210
fynyky Avatar asked Dec 06 '25 06:12

fynyky


1 Answers

I would do it using NSExpression. This bit of code below won't work for you because you will have to group by username too, but it's a start for the best way of doing this without having to fetch everything. You want to perform the max and group in the db, not in memory - as it will be faster:

NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];

// Expression for the max
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"timestamp"];

NSEntityDescription *entity = [NSEntityDescription entityForName:entityName
                                          inManagedObjectContext:self.coreDataStack.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setResultType:NSDictionaryResultType];

NSExpression *valueSumExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]];

NSExpressionDescription *expressionDescription = [[[NSExpressionDescription alloc] init] autorelease];
[expressionDescription setName:@"maxTimestamp"];
[expressionDescription setExpression:valueSumExpression];
[expressionDescription setExpressionResultType:NSDecimalAttributeType];

[fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];

// Filter
//NSPredicate *pred = [NSPredicate predicateWithFormat:@" your predicate here"];
//fetchRequest.predicate = pred;

NSArray *results = [self.coreDataStack.managedObjectContext executeFetchRequest:fetchRequest error:nil];
if([results count] == 0) {

} else {

    // [[results objectAtIndex:0] valueForKey:@"maxTimestamp"];

}
like image 88
bandejapaisa Avatar answered Dec 08 '25 20:12

bandejapaisa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!