Ios NSFetchedResultsController将重复节添加到UICollectionView

Ios NSFetchedResultsController将重复节添加到UICollectionView,ios,swift,core-data,uicollectionview,nsfetchedresultscontroller,Ios,Swift,Core Data,Uicollectionview,Nsfetchedresultscontroller,我有一个NSFetchedResultsController,它从CoreData获取对象,并更新UICollectionView以相应地显示数据。UICollectionView中的数据根据CoreData关系中的属性组织在各个部分中。这种关系如下所示 // an Item belongs to exactly one Category - a Category can host multiple Items Item <<---> Category 一旦我关闭应用程序并重

我有一个
NSFetchedResultsController
,它从
CoreData
获取对象,并更新
UICollectionView
以相应地显示数据。
UICollectionView
中的数据根据
CoreData
关系中的属性组织在各个部分中。这种关系如下所示

// an Item belongs to exactly one Category - a Category can host multiple Items
Item <<---> Category
一旦我关闭应用程序并重新启动它,新的部分就会消失,所有的
项都位于正确的部分中-可能是
NSFetchedResultsController
有问题,下面是重要的代码

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
    if type == NSFetchedResultsChangeType.insert {
        blockOperations.append(
            BlockOperation(block: { [weak self] in
                if let this = self {
                    this.collectionView!.insertSections(NSIndexSet(index: sectionIndex) as IndexSet)
                    
                }
                })
        )
    }
    else if type == NSFetchedResultsChangeType.update {
        blockOperations.append(
            BlockOperation(block: { [weak self] in
                if let this = self {
                    this.collectionView!.reloadSections(NSIndexSet(index: sectionIndex) as IndexSet)
                }
                
                })
        )
    }
    else if type == NSFetchedResultsChangeType.delete {
        blockOperations.append(
            BlockOperation(block: { [weak self] in
                if let this = self {
                    this.collectionView!.deleteSections(NSIndexSet(index: sectionIndex) as IndexSet)
                } 
                })
        )
    }
}

您能告诉我们如何实例化
NSFetchedResultsController
并开始它的获取吗?我用NSFetchedResultsController的实例化方法更新了这个问题,并不是说这会有什么不同,而是出于好奇,如果在调用performFetch(而不是之前设置)之后设置委托会发生什么情况。感谢您的评论,不幸的是,在performFetch之后设置代理没有任何区别。您是否找到了解决方案?
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
    if type == NSFetchedResultsChangeType.insert {
        blockOperations.append(
            BlockOperation(block: { [weak self] in
                if let this = self {
                    this.collectionView!.insertSections(NSIndexSet(index: sectionIndex) as IndexSet)
                    
                }
                })
        )
    }
    else if type == NSFetchedResultsChangeType.update {
        blockOperations.append(
            BlockOperation(block: { [weak self] in
                if let this = self {
                    this.collectionView!.reloadSections(NSIndexSet(index: sectionIndex) as IndexSet)
                }
                
                })
        )
    }
    else if type == NSFetchedResultsChangeType.delete {
        blockOperations.append(
            BlockOperation(block: { [weak self] in
                if let this = self {
                    this.collectionView!.deleteSections(NSIndexSet(index: sectionIndex) as IndexSet)
                } 
                })
        )
    }
}
    var _fetchedResultsController: NSFetchedResultsController<Item>? = nil

var fetchedResultController: NSFetchedResultsController<Item> {
    if _fetchedResultsController != nil {
        return _fetchedResultsController!
    }
    
    let fetchRequest: NSFetchRequest<Item> = Item.fetchRequest()
    let managedObjectContext = CoreDataStack.sharedInstance.getContext()
    
    fetchRequest.predicate = NSPredicate(format: "completed != true")

    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "category", ascending: true)]
    let resultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath:"category", cacheName: nil)
    
    resultsController.delegate = self;
    _fetchedResultsController = resultsController
    
    do {
        try _fetchedResultsController!.performFetch()
    } catch {
        let nserror = error as NSError
        fatalError("Unresolved error \(nserror)")
    }
    return _fetchedResultsController!
}