6

I want the maximum value of a field in core data so I have set up the following request:

// Create fetch
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:context]];
[fetch setResultType:NSDictionaryResultType];

// Expression for Max ID
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"myNumbericID"];
NSExpression *minExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"maxID"];
[expressionDescription setExpression:minExpression]; 
[expressionDescription setExpressionResultType:NSDoubleAttributeType];
[fetch setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];

// Execute the fetch. 
double theID = 0;
NSError *error;
NSArray *objects = [context executeFetchRequest:fetch error:&error]; 
if (objects && [objects count] > 0) { 
    theID = [((NSNumber *)[[objects objectAtIndex:0] valueForKey:@"maxID"]) doubleValue];
}

However it would appear it's not taking into account any new objects that have been inserted into the context before hand. Is this how it is supposed to work? Does it only work on objects in the store?

I can't find any reference to it in the docs.

Thanks,

Mike

Michael Waterfall
  • 20,497
  • 27
  • 111
  • 168

3 Answers3

7

I've had a response from the Apple Developer Forums and it was confirmed that it doesn't take into account unsaved changes.

Michael Waterfall
  • 20,497
  • 27
  • 111
  • 168
1

This behaviour is documented (poorly) in [NSFetchRequest setIncludePendingChanges:]. A value of YES is not supported with NSDictionaryResultType. This means that you cannot use NSExpressionDescription on unsaved changes.

Aderstedt
  • 6,301
  • 5
  • 28
  • 44
0

FYI, you can skip the whole NSExpression section and just go ahead with the fetch, then use collection operators for the max value.

theID = [objects valueForKeyPath:"@max.myNumbericID"];
  • 2
    While this would work, it would rely on bringing every object that meets the criteria into memory first. In my case this would be far too many objects. I needed Core Data to compute this via an aggregate SQL statement against the data store. – Michael Waterfall Jan 13 '11 at 10:35
  • In that case you can just use a NSSortDescriptor and a fetchLimit 1, then you only load in the single object. – malhal Jul 17 '16 at 18:26