Search code examples
iosobjective-cswiftcore-datansfetchedresultscontroller

NSFetchedResultsController with non unique section name


I am initialising NSFetchedResultsController with following code

self.fetchedResultsController = NSFetchedResultsController(
            fetchRequest: request,
            managedObjectContext: context,
            sectionNameKeyPath: "article.name",cacheName: nil
        )

Here articles can have same name.So article.name sectionNameKeyPath is non unique.In this case fetchedResultsController will have only single section.

How can I handle multiple section with same section name?


Solution

  • For the sectionKeyPath return a UUID for the section (article.articleId or the like). Then when you display the title for the header in section don't display the named returned from the fetchedResultsController (self.fetchedResultsController.sections[section].name), rather get the first object in the section and figure out what you really want to display based on that (self.fetchedResultsController.sections[section].objects.firstObject.article.name).