Kernel master data: NSFetchRequest with individual properties and alphabet sections

I am trying to create an NSFetchRequest that gives results for a UITableView.

It should find all the individual occurrences of the NSString property and split them into sections of the alphabet ("A", "B", "C", etc.)

I set a method for 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 it works fine

  • However, now I need to set returnDistinctResults to YES

  • But, returnsDistinctResults only works if the ToFetch property is set, so

  • I set the ToFetch properties to "entrant" (the property that interests me)

  • , ToFetch , resultType NSDictionaryResultType,

  • resultType NSDictionaryResultType

  • Type , "sectionNameKeyPath" @ "entrantFirstLetter" .

, , , "entrantFirstLetter" ToFetch? NSEntityDescription, !

, , Core Data . , , , .

,

.

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];
+3
2

NSFetchResultsController's.

sectionNameKeyPath: entrant, FRC , .

+1

, , NSFetchResultsController ( SDK 3.1.3). NSFetchRequest loadView, .

, Apple , 4.x, .

+1

Source: https://habr.com/ru/post/1735808/


All Articles