Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone Core Data: NSFetchRequest with distinct properties and alphabet sections

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];
like image 357
Russell Quinn Avatar asked Nov 18 '25 00:11

Russell Quinn


1 Answers

You are trying to duplicate the NSFetchResultsController's default section behavior.

Instead, just set the sectionNameKeyPath: to entrant and the FRC will create the sections for you with no more effort on your part.

like image 173
TechZen Avatar answered Nov 20 '25 15:11

TechZen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!