Swift 如何在视图上重新加载fetchedResultsController?

Swift 如何在视图上重新加载fetchedResultsController?,swift,core-data,nsfetchedresultscontroller,Swift,Core Data,Nsfetchedresultscontroller,如何在视图上重新加载fetchedResultsController?因为我想在关闭弹出视图控制器后重新加载表视图和数据 此fetch控制器似乎仅在加载视图时运行,因为它保留旧数据。当视图出现时,如何运行或刷新fetch控制器 var fetchedResultsController: NSFetchedResultsController<Record> { if _fetchedResultsController != nil { return _fetch

如何在视图上重新加载fetchedResultsController?因为我想在关闭弹出视图控制器后重新加载表视图和数据

此fetch控制器似乎仅在加载视图时运行,因为它保留旧数据。当视图出现时,如何运行或刷新fetch控制器

var fetchedResultsController: NSFetchedResultsController<Record> {
    if _fetchedResultsController != nil {
        return _fetchedResultsController!
    }

    let fetchRequest: NSFetchRequest<Record> = Record.fetchRequest()
    fetchRequest.fetchBatchSize = 20
    let accBtn = UserDefaults().value(forKey: "BtnName") as! String
    let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: false)
    fetchRequest.sortDescriptors = [sortDescriptor]
    fetchRequest.predicate = NSPredicate(format: "%K == %@", "accountbook" , "\(accBtn)")
    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: "createdAt", cacheName: nil)
    aFetchedResultsController.delegate = self
    _fetchedResultsController = aFetchedResultsController

    do {
        try _fetchedResultsController!.performFetch()
    } catch {
        let nserror = error as NSError
        fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
    }

    return _fetchedResultsController!
}

var _fetchedResultsController: NSFetchedResultsController<Record>? = nil

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    self.tableView.beginUpdates()
}

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
    switch type {
    case .insert:
        self.tableView.insertSections(IndexSet(integer: sectionIndex), with: .fade)
    case .delete:
        self.tableView.deleteSections(IndexSet(integer: sectionIndex), with: .fade)
    default:
        return
    }
}

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
    switch type {
    case .insert:
        tableView.insertRows(at: [newIndexPath!], with: .fade)
    case .delete:
        tableView.deleteRows(at: [indexPath!], with: .fade)
    case .update:
        self.configureCell(tableView.cellForRow(at: indexPath!)! as! DashboardCell, withRecordItem: anObject as! Record)
    case .move:
        tableView.moveRow(at: indexPath!, to: newIndexPath!)
    }
}

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    self.tableView.endUpdates()
}
var fetchedResultsController:NSFetchedResultsController{
如果_fetchedResultsController!=nil{
return\u fetchedResultsController!
}
let fetchRequest:NSFetchRequest=Record.fetchRequest()
fetchRequest.fetchBatchSize=20
将accBtn=UserDefaults().value(forKey:“BtnName”)设为!字符串
让sortDescriptor=NSSortDescriptor(键:“createdAt”,升序:false)
fetchRequest.sortDescriptors=[sortDescriptor]
fetchRequest.predicate=NSPredicate(格式:“%K==%@”,“账簿”,“\(accBtn)”)
设aFetchedResultsController=NSFetchedResultsController(fetchRequest:fetchRequest,managedObjectContext:self.managedObjectContext!,sectionNameKeyPath:“createdAt”,cacheName:nil)
aFetchedResultsController.delegate=self
_fetchedResultsController=aFetchedResultsController
做{
请尝试_fetchedResultsController!.performFetch()
}抓住{
设nserror=错误为nserror
fatalError(“未解决的错误\(nserror),\(nserror.userInfo)”)
}
return\u fetchedResultsController!
}
var\u fetchedResultsController:NSFetchedResultsController?=无
func controllerWillChangeContent(\ucontroller:NSFetchedResultsController){
self.tableView.beginUpdate()
}
func控制器(控制器:NSFetchedResultsController,didChange sectionInfo:NSFetchedResultsSectionInfo,atSectionIndex sectionIndex:Int,类型:NSFetchedResultsChangeType){
开关类型{
案例.插入:
self.tableView.insertSections(IndexSet(整数:sectionIndex),带:.fade)
案例.删除:
self.tableView.deleteSections(IndexSet(整数:sectionIndex),带:.fade)
违约:
返回
}
}
func控制器(controller:NSFetchedResultsController,didChange anObject:Any,在indexPath:indexPath?处,对于类型:NSFetchedResultsChangeType,newIndexPath:indexPath?){
开关类型{
案例.插入:
tableView.insertRows(位于:[newIndexPath!],带:.fade)
案例.删除:
tableView.deleteRows(位于:[indexPath!],带:.fade)
案例。更新:
self.configureCell(tableView.cellForRow(at:indexPath!)!as!DashboardCell,withRecordItem:anObject as!Record)
案件.动议:
tableView.moveRow(at:indexPath!,to:newIndexPath!)
}
}
func controllerDidChangeContent(\控制器:NSFetchedResultsController){
self.tableView.endUpdates()
}

如果在更改托管对象上下文中的数据时ViewController在内存中,则获取的结果控制器委托应该可以工作

在当前的实现中,每次更改时都会更新tableview,但这样做并不总是可取的。我想您需要离题,只在上面实现
didChangeContent
reloadData

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    self.tableView.reloadData()
}
func controllerDidChangeContent(\ucontroller:NSFetchedResultsController){
self.tableView.reloadData()
}

我试过这个。在我关闭pop vc后,表被刷新,但列表有问题(一些数据没有显示),当我选择单元格时,我遇到了这个错误
“在-performFetch:”之前无法访问获取的对象。
@WanJern:您删除了所有其他委托方法吗?您的意思是我必须删除-
controllerWillChangeContent,是否更改节和对象
?但我需要它来更新我的UI并将同一日期项分组到一个部分中@Puneet SharmaYes,我是认真的。在didChangeContent上调用tableView上的reloadData将执行所有必需的更改。好的,我尝试删除它,但在我关闭弹出窗口vc后它仍显示旧数据…我遇到了相同的问题。。。有什么解决办法吗?