Nil relationship value for section name key path in nsfetchedresultscontroller

For a core data model where EntityA has a many-to-one relationship with EntityB, I want to create a list of EntityA objects, sorted by the name of the EntityB they are related to. Normally to do this I would set up the fetch request like this:

if (_fetchedResultsController != nil) {
    return _fetchedResultsController;
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

Then I would set up my request's values: (in this case it's a list of plants sorted by species name. some plants have no species set.)

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Plant" inManagedObjectContext:self.managedObjectContext];
NSSortDescriptor *sortDescriptorOne = [[NSSortDescriptor alloc] initWithKey:@"species.name" ascending:YES];
NSString *sectionKeyPath = @"species.name";

Then I finish it up with the usual stuff:

[fetchRequest setEntity:entity];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

NSArray *sortDescriptors = @[sortDescriptorOne];
[fetchRequest setSortDescriptors:sortDescriptors];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:sectionKeyPath
cacheName:@"plantsCache"];

aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

[NSFetchedResultsController deleteCacheWithName:@"plantsCache"];
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

return _fetchedResultsController;

But the result I get isn't working for me because the relationship is optional. So some of EntityA have a relationship to EntityB's and some don't. The results controller doesn't seem to know what to do when the value of EntityA's relationship is nil.

Any suggestions what I can do to continue making sections using the relationship's value but allowing some objects to have nil for it?

Answers


Set up a transient property in Plant like this:

-(NSString*)speciesName {
    return self.species ? self.species.name : @"";
}

You can now use speciesName as the sectionNameKeyPath.


Need Your Help

Configuring WCF client and service for use with protobuf-net

wcf protobuf-net datacontractserializer custom-binding

I decided to open a new question about this matter, maybe expanding this question, not having found a precise answer about the issue anywhere on the Internet.

Ajax PHP Live Search - Second Step Needed

php mysql ajax search live

I have a Ajax PHP MySQL live search that basically pulls out food items from a MySQL database and presents them in a drop-down list, as users enter they search term, one item per line, just like

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.