NSFetchedResultsController performFetch 不起作用

NSFetchedResultsController performFetch doesn't work

我在屏幕上进行了分段控制。 1 个分段项目用于广告 (Ads table),第二个分段项目用于订单 (Orders table)。 每次当用户选择分段项目时,我都会重新创建 NSFetchResultController 的实例,并使用对另一个数据的新获取请求 table (但在 UI 上我总是只有一个 UITableView self.tableViewMain):

let entityName = screen == ProfileScreen.Ads ? kDB_entity_DBAd : kDB_entity_DBOrder
let entityDescription = NSEntityDescription.entityForName(entityName, inManagedObjectContext: (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext)
let fetchRequest = NSFetchRequest()
fetchRequest.entity = entityDescription
self.fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: (UIApplication.sharedApplication().delegate! as! AppDelegate).managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
do {
    try fetchedResultsController!.performFetch()
} catch {
    print("An error occurred in fetchresultcontroller")
}

但是第一次它正确地重新加载了数据,但是第二次 fetchresultcontroller 甚至没有重新加载 table(numberOfRowsInSection and cellForRowAtIndexPath 没有被调用)。因此,当我调用 fetchedResultsController!.performFetch()

时,NSFetchResultController 的第二次新实例(与核心数据中的第二个 table 相关)没有做任何事情

您应该调用 tableViewMain.reloadData() 让 table 重新加载自己。