mergesort
mergesort

Reputation: 5197

How to get core data distinct values with relationship values?

I am trying to get all the distinct Identity values (contact has many identities). I am also trying to get the contact.sectionIndex from the contact model. I am trying the code below and get "returned nil value for section name key path 'Contact.sectionIndex'" for each record.

NSFetchRequest *req = NSFetchRequestMake(@"Identity", managedObjectContext);
req.fetchBatchSize = 20;
req.sortDescriptors = NSSortDescriptors1(@"sortname", YES);
[req setResultType:NSDictionaryResultType];
[req setReturnsDistinctResults:YES];
[req setPropertiesToFetch:@[@"identityID", @"sortname"]];


// Create & return the fetchedResultsController.
fetchedResultsController = [[NSFetchedResultsController alloc]
                            initWithFetchRequest:fetchRequest
                            managedObjectContext:managedObjectContext
                            sectionNameKeyPath:@"contact.sectionIndex" cacheName:nil];
[VenmoUtilities performFetch:fetchedResultsController critical:NO];

Upvotes: 0

Views: 767

Answers (2)

feliun
feliun

Reputation: 181

From Apple doc you can see you may asking too much from the request.

Discussion

The property descriptions may represent attributes, to-one relationships, or expressions. The name of an attribute or relationship description must match the name of a description on the fetch request’s entity.

Upvotes: 0

Martin R
Martin R

Reputation: 539795

The property used as sectionNameKeyPath must be included in the propertiesToFetch:

[req setPropertiesToFetch:@[@"identityID", @"sortname", @"contact.sectionIndex"]];

Upvotes: 1

Related Questions