NSFetchedResultsController 部分编号为 0 [英] NSFetchedResultsController section number is 0

查看:64
本文介绍了NSFetchedResultsController 部分编号为 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下实体层次结构

在我的 TableViewController 中,我有以下代码:

In my TableViewController i have the following code:

private lazy var fetchedResultsController: NSFetchedResultsController = {
    let fetchRequest = NSFetchRequest(entityName: "Worker")
    let workerTypeSortDescriptor = NSSortDescriptor(key: "workerType", ascending: true)
    let firstNameSortDescriptor = NSSortDescriptor(key: "firstName", ascending: true)

    fetchRequest.sortDescriptors = [workerTypeSortDescriptor,  firstNameSortDescriptor]
    guard let stack = CoreDataStackSingleton.sharedInstance.coreDataStack else {return NSFetchedResultsController()}
    let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: stack.mainQueueContext, sectionNameKeyPath: "workerType", cacheName: nil)
    frc.delegate = self
    return frc
}()
...
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    if let sections = fetchedResultsController.sections {
        print(sections.count)
        return sections.count
    }
    return 0
}
...
func viewDidLoad(){
    ...
    try fetchedResultsController.performFetch()
    ...
}

而且我总是从 print(sections.count) 得到 0 我不知道为什么,因为我有 3 个实体并且我假设得到 3 个部分.这是项目

And i always get the 0 from print(sections.count) i don't know why, because i have 3 entities and i assume to get 3 sections. Here is the project

推荐答案

假设您的 coredata 堆栈正常工作,并且您在上下文中为每种应该工作的类型插入了记录.但是,如果您的上下文为空,那么我希望该部分计数为 0.

Assuming you have have your coredata stack working and you have records inserted in the context for each type this should work. But if your context is empty then I would expect the section counts to be 0.

这篇关于NSFetchedResultsController 部分编号为 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆