Search code examples
ioscore-datamobilensfetchedresultscontrollernsmanagedobject

Is it possible to use a sectionKeyPath with NSFetchedResultsController when fetching properties with NSDictionaryResultType?


I've got a fetch request set up to retrieve some properties on 'User' items:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:companyName, firstName, lastName, completeItems, incompleteItems, objectID, nil]];
[fetchRequest setPredicate:predicate];
[fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:SortOptionCompany ascending:YES],
                                   [NSSortDescriptor sortDescriptorWithKey:SortOptionFirstName ascending:YES]]];
[fetchRequest setFetchBatchSize:kBatchSize];

I initialize my NSFetchedResultsController:

NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                      managedObjectContext:self.context
                                                                        sectionNameKeyPath:@"sectionIdentifier"
                                                                                 cacheName:nil];

The attribute "sectionIdentifier" is a transient property on the User entity. However, when I execute this, I get the message:

"returned nil value for section name key path 'sectionIdentifier'. Object will be placed in unnamed section"

The fetched information isn't grouped into sections as desired. Is this because I need to fetch whole NSManagedObjects for the keyPath to not be nil, or is there a way to use sectionKeyPath when retrieving dictionaries? I've also tried using setPropertiesToGroupBy: with no success.


Solution

  • Since there was no solution that I could find for this issue, I resorted to not using a sectionKeyPath and sorting the data into sections manually after retrieval.