首页 > 解决方案 > Swift NSFetchedResultsController Sections 总是返回 nil

问题描述

我对 swift 很陌生,我正在尝试制作一个应用程序来学习它。我创建了一个简单的应用程序来存储一些书籍的信息,然后将其显示在CollectionView上。

我制作了一个简单的数据模型,它有一个 Books (Livro) 实体,其中包含一些属性,包括类别、标题和图像(显示在 CV 上)。我想显示然后分成几个部分(按类别 - 类别)。

我的问题是我可以从 Data-Core 获取数据,我可以在控制台上列出所有这些,但是 fetchResultsController.sections 总是返回 nil,即使“categoria”属性有一些值。

调用了委托 CV 方法 numberOfSections(在集合视图中),但它返回 0,并且从不调用 de CV 委托 cellForItemAt。

这是我的 UIViewController 上的一些代码:

    override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    ListaLivros.delegate = self
    ListaLivros.dataSource = self

    if let context = container?.viewContext {
        let sortDescriptor = NSSortDescriptor(key: "titulo", ascending: true, selector: #selector(NSString.localizedCaseInsensitiveCompare(_:)))
        let sortDescriptorSections = NSSortDescriptor(key: "categoria", ascending: true, selector: #selector(NSString.localizedCaseInsensitiveCompare(_:)))

        let request: NSFetchRequest<Livro> = Livro.fetchRequest()
        request.sortDescriptors = [sortDescriptor, sortDescriptorSections]
        request.predicate = nil

        fetchedResultsController = NSFetchedResultsController<Livro>(
            fetchRequest: request,
            managedObjectContext: context,
            sectionNameKeyPath: "categoria",
            cacheName: nil)
        fetchedResultsController?.delegate = self

        //------______---======   PRINT BOOKS   ------______---======
        if let results = try? context.fetch(Livro.fetchRequest()) {

            for livro in results {
                print ("  -> Secoes: \(String(describing: fetchedResultsController?.sections))")
                print ("   --> Livro: \(livro)")
//                    context.delete(livro as! NSManagedObject)
//                    try? context.save()
            }
        }
        //------______---======   TESTE   ------______---======
    }

    atualizaView()
    mostraBDStats ()
} // DIDLOAD

我将委托功能全部放在扩展上。CoreData 部分似乎工作正常,因为它在 DataCore 上插入数据。所以,我只发布我的简历扩展:

extension ListaLivrosVC: UICollectionViewDelegate, UICollectionViewDataSource {
    func numberOfSections( in collectionView: UICollectionView) - > Int {
        print("Num Sec: (String(describing: fetchedResultsController?.sections?.count))")

    if let context = container?.viewContext {
        //------______---======   PRINT BOOKS   ------______---======
        if let results = try? context.fetch(Livro.fetchRequest()) {
            for livro in results {
                print ("  -> 2Secoes: \(String(describing: fetchedResultsController))")
                print ("   --> 2Livro: \(livro)")
                //                    context.delete(livro as! NSManagedObject)
                //                    try? context.save()
            }
        }
    }
    return fetchedResultsController?.sections?.count ?? 0
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    guard let sections = fetchedResultsController?.sections else { return 0 }

    if !PickerSecoes.contains(sections[section].name) {
        PickerSecoes.append(sections[section].name)
    }

    return sections[section].numberOfObjects

} // COLLECTION

//--------------------------------------------------------------------
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let celula = ListaLivros.dequeueReusableCell(withReuseIdentifier: "LivrosCell", for: indexPath)

    if let LivroCelula = celula as? LivroCollectionCell {
        if let livro = fetchedResultsController?.object(at: indexPath) {
            LivroCelula.Imagem.image = livro.getImagem()
            LivroCelula.Titulo.text = livro.titulo
        } // if
    } // IF

    return celula
} // COLLECTION
}

我不知道我还能做什么。运行应用程序,我可以看到我插入的 2 本书,但它们没有出现在 CV 上:

Numero de Livros no BD: 2
  -> Secoes: nil
   --> Livro: Titulo: Optional("teste"), Autores: Optional("teste123"), Categoria: Optional("testetestes"), Tags: nil
  -> Secoes: nil
   --> Livro: Titulo: Optional("teste2"), Autores: Optional("tests do"), Categoria: Optional("casa"), Tags: nil
Num Sec: nil
  -> 2Secoes: Optional(<NSFetchedResultsController: 0x600003382700>)
   --> 2Livro: Titulo: Optional("teste"), Autores: Optional("teste123"), Categoria: Optional("testetestes"), Tags: nil
  -> 2Secoes: Optional(<NSFetchedResultsController: 0x600003382700>)
   --> 2Livro: Titulo: Optional("teste2"), Autores: Optional("tests do"), Categoria: Optional("casa"), Tags: nil
Numero de Livros no BD: 2

只是为了补充,在同一个应用程序中,我还有另外两个类似的实体。我可以成功地在带有部分的 TableView 上列出它们。上面的代码是从他们那里复制过来的,并适合使用 CollectionView。

我正在使用 XCode 10、Swift 4.2

标签: iosswiftnsfetchedresultscontroller

解决方案


回答我自己的问题,我想我意识到出了什么问题,尽管我不知道为什么它是错误的,以及为什么它与另一个带有 TableView 的实体一起工作。

这个问题在我看来是谓词。评论该行:

    request.predicate = nil

似乎可以解决问题。


推荐阅读