When working with a table view controller, an unnamed section is generated by the NSFetchedResultsController for those items in my CoreData model that lack a certain relationship ('belongsToCategory').
In fact, this is almost exactly the behavior I want - I would rather use a special name instead of an empty one, but I can easily rename the empty section name on the fly in tableView(_:titleForHeaderInSection:).
But how do I get rid of the error message in the log in the first place?
CoreData: error: (NSFetchedResultsController) A section returned nil value for section name key path 'belongsToCategory.name'. Objects will be placed in unnamed section
Can I override some method or anything in order to return a default name if the relationship is not set?
Found the solution myself:
extension Product {
@objc var computedName:String { // @objc is crucial for making it KVC compliant!
get {
if belongsToCategory != nil {
return belongsToCategory!.name!
} else {
return "special"
}
}
}
}
In the NSFetchedResultsController use "computedName" for section name key path instead of "belongsToCategory.name".
Data model here is: entity "Product" <<-- "belongsToCategory" ---> entity "Category". Both entities have name attributes.