I want the maximum field value in the kernel data, so I set the following query:
// 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 seem that it does not take into account any new objects that were inserted into the context before distribution. Is this the way it should work? Does it work only on objects in the store?
I can not find links to it in the docs.
Thanks,
Mike
core-data nsfetchrequest
Michael waterfall
source share