Swift NSFetchedResultsController not sectioning based on sectionNameKeyPath

Viewed 86
lazy var fetchedResultsController: NSFetchedResultsController<StoryTemplatePack> =
    {
        let managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).managedObjectContext

        var frc: NSFetchedResultsController<StoryTemplatePack> = NSFetchedResultsController(fetchRequest: self.createFetchRequest(), managedObjectContext: managedObjectContext, sectionNameKeyPath: "sectionnum", cacheName: nil)
        frc.delegate = self

        return frc
}()

// FetchedResultsController: Create Request
func createFetchRequest() -> NSFetchRequest<StoryTemplatePack> {
    let fetchRequest = NSFetchRequest<StoryTemplatePack>(entityName: "StoryTemplatePack")

    fetchRequest.predicate = NSPredicate.init(format: "productidentifier != nil")

    fetchRequest.sortDescriptors = [
        NSSortDescriptor(key: "isfeatured", ascending: false),
        NSSortDescriptor(key: "ispurchased", ascending: true),
        NSSortDescriptor(key: "name", ascending: true)
    ]


    return fetchRequest
}

The results should be sectioned according to the sectionnum property on StoryTemplatePack (which is a transient property calculated in code).

When I loop over the results of the NSFetchedResultsController it shows the correct section numbers and story pack names. There should be 3 sections, but the FRC is reporting only 1 section. The dumped results also show that I'm sorting the results correctly so that the section values are monotonically increasing.

---- dumping frc, num sections: 1
    section 0 pack Incredible Journeys
    section 0 pack Out for a Date
    section 0 pack Party Time
    section 1 pack Holiday Craziness
    section 1 pack In the Office
    section 1 pack On the Internets
    section 1 pack On the Job
    section 1 pack School Daze
    section 1 pack Social Networking
    section 1 pack TV Troubles
    section 1 pack Technology Takes Over
    section 2 pack Christmas Shenanigans
---- done dumping

self.fetchedResultsController.sections!.count is returning 1 when it should return 3.

What am I doing wrong or missing?

0 Answers
Related