Created
November 4, 2012 05:05
-
-
Save RobvH/4010398 to your computer and use it in GitHub Desktop.
Core Data, using NSExpressions, how can we get these results to also include Department names?
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
- (void)fetchDepartmentExpenditureTotals | |
{ | |
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Department"]; | |
// get all departments | |
request.predicate = nil; | |
// perhaps a second fetch could get the names and sort the same way, but this feels kludgy | |
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; | |
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; | |
// Expenditure is another Entity with total as an attribute, linked on Department as expenditure | |
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"expenditure.total"]; | |
// select sum(expenditure.total) | |
NSExpression *sumExpression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:keyPathExpression]]; | |
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; | |
[expressionDescription setName:@"DepartmentTotal"]; | |
[expressionDescription setExpression:sumExpression]; | |
[expressionDescription setExpressionResultType:NSDecimalAttributeType]; | |
// select Department.name | |
NSExpression *nameExpression = [NSExpression expressionForKeyPath:@"name"]; | |
NSExpressionDescription *nameExpressionDescription = [[NSExpressionDescription alloc] init]; | |
[nameExpressionDescription setName:@"DepartmentName"]; | |
[nameExpressionDescription setExpression:nameExpression]; | |
[nameExpressionDescription setExpressionResultType:NSStringAttributeType]; | |
[request setResultType:NSDictionaryResultType]; | |
[request setPropertiesToFetch:[NSArray arrayWithObjects:sumExpressionDescription, | |
nameExpressionDescription, | |
@"assignedChartingColor", // this is a relationship | |
nil]]; | |
NSError *error = nil; | |
NSArray *results = (NSDictionary *)[self.managedObjectContext executeFetchRequest:request error:&error]; | |
if (error) { | |
// handle error | |
return; | |
} | |
// ... | |
} | |
- (UIColor *)chartingColorForDepartmentWithIndex:(NSUInteger)index | |
{ | |
NSArray *resultsArray = self.data; // the results array of the fetch above | |
NSDictionary *resultsRowDictionary = [resultsArray objectAtIndex:index]; | |
// Color is the managed object of the entity that assignedChartingColor links to | |
Color *colorManagedObject = [resultsRowDictionary objectForKey:@"assignedChartingColor"]; | |
NSLog(@"%@", resultsRowDictionary); | |
/* returns: | |
* DepartmentName = Orange; | |
* DepartmentTotal = 12; | |
* assignedChartingColor = "0x74d6540 <x-coredata://F0E24183-4D4F-46E1-8224-FC4D1496457B/Color/p5>"; | |
* :( :( :( | |
*/ | |
// this would be what I'd want to do, but for now the objectForKey:@"assignedChartingColor" isn't returning a Color* | |
UIColor *thisColor = colorManagedObject.color; // .color is a transient UIColor* | |
return (thisColor ? thisColor : [UIColor clearColor]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment