Last active
December 30, 2015 19:39
-
-
Save jchudzynski/7875611 to your computer and use it in GitHub Desktop.
Getting a min value of CoreData entity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSFetchRequest *request = [[NSFetchRequest alloc] init]; | |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Slide" inManagedObjectContext:[[manager getAppDelegate]managedObjectContext]]; | |
[request setEntity:entity]; | |
// Specify that the request should return dictionaries. | |
[request setResultType:NSDictionaryResultType]; | |
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"order"]; | |
// Create an expression to represent the minimum value at the key path 'creationDate' | |
NSExpression *minExpression = [NSExpression expressionForFunction:@"min:" arguments:[NSArray arrayWithObject:keyPathExpression]]; | |
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; | |
// The name is the key that will be used in the dictionary for the return value. | |
[expressionDescription setName:@"minDate"]; | |
[expressionDescription setExpression:minExpression]; | |
[expressionDescription setExpressionResultType:NSObjectIDAttributeType]; | |
// Set the request's properties to fetch just the property represented by the expressions. | |
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]]; | |
// Execute the fetch. | |
NSError *error = nil; | |
NSArray *objects = [[[manager getAppDelegate]managedObjectContext] executeFetchRequest:request error:&error]; | |
if (objects == nil) { | |
// Handle the error. | |
} | |
else { | |
if ([objects count] > 0) { | |
NSLog(@"Object %@",[objects objectAtIndex:0]); | |
NSLog(@"Minimum date: %@", [[objects objectAtIndex:0] valueForKey:@"minDate"]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment