Hello everyone,
I am trying to create a NSFetchRequest that produces results for a UITableView.
It should find all the distinct occurrences of a NSString property and split them into alphabet sections ('A', 'B', 'C', etc.)
I have set up a method on my NSManagedObject class to return the first letter of the property:
- (NSString *)entrantFirstLetter
{
[self willAccessValueForKey:@"entrantFirstLetter"];
NSString *returnString = [self.entrant substringToIndex:1];
[self didAccessValueForKey:@"entrantFirstLetter"];
return returnString;
}
I set 'sectionNameKeyPath' to @"entrantFirstLetter" and this works perfectly
However, I now need to set returnsDistinctResults to YES
But, returnsDistinctResults only works if propertiesToFetch is set, so
I set propertiesToFetch to "entrant" (the property I'm interested in)
But, in order for propertiesToFetch to work, the resultType must be NSDictionaryResultType, so
I set resultType to NSDictionaryResultType
But, this resultsType means that my 'sectionNameKeyPath' of @"entrantFirstLetter" no longer works.
So, you'd think the answer was to add in 'entrantFirstLetter' to the propertiesToFetch? But as it's not a property on the NSEntityDescription I can't!
All I want to do is sort a list of strings in Core Data into alphabet sections and not have duplicates. I can get each part working on its own, but getting it all working together relies on a seemingly endless loop of dependencies and I can't find a way to get it all working.
Any ideas would be much appreciated,
Russell.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSDictionary *properties = [entity propertiesByName];
NSArray *propertiesToFetch = [NSArray arrayWithObject:[properties objectForKey:@"entrant"]];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:propertiesToFetch];
[fetchRequest setReturnsDistinctResults:YES];
NSSortDescriptor *entrantDescriptor = [[NSSortDescriptor alloc] initWithKey:@"entrant" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:entrantDescriptor]];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext sectionNameKeyPath:@"entrantFirstLetter" cacheName:nil];