Swift NSFetchedResultsController不基于sectionNameKeyPath进行分段

时间:2019-06-18 23:55:23

标签: ios swift nsfetchedresultscontroller

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
}

应根据StoryTemplatePack的sectionnum属性(该结果是用代码计算的瞬态属性)来划分结果的。

当我遍历NSFetchedResultsController的结果时,它将显示正确的段号和故事包名称。应该有3个部分,但FRC仅报告了1个部分。转储的结果还表明,我对结果进行了正确排序,以使截面值单调增加。

---- 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在应返回3时返回1。

我做错了什么或想念什么?

0 个答案:

没有答案