如何在viewWillAppear上重新加载fetchedResultsController?

时间:2017-11-16 05:20:07

标签: swift core-data nsfetchedresultscontroller

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

此提取控制器似乎仅在视图加载时运行,因为它保留旧数据。如何在显示视图时运行或刷新提取控制器?

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()
}

1 个答案:

答案 0 :(得分:1)

如果在更改托管对象上下文中的数据时ViewController在内存中,则提取的结果控制器委托应该有效。

在当前的实现中,您将在每次更改时更新tableview,并且始终不建议执行此更改。我想你需要离开并只实现didChangeContentreloadData

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