Paul T.
Paul T.

Reputation: 5038

NSFetchedResultsController performFetch doesn't work

I have segmented control on the screen. 1 segmented item is for Ads (Ads table) and the 2nd segmented item is for Orders (Orders table). Each time when user selects the segmented item, I recreate the instance of NSFetchResultController with new fetch request to another data table (but on UI I always have only one 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")
}

But first time it reloads data correctly, but second time fetchresultcontroller even doesn't reload table (numberOfRowsInSection and cellForRowAtIndexPath are not called). So the second time new instance of NSFetchResultController (which is related to the second table in core data) doesn't do anything when I call fetchedResultsController!.performFetch()

Upvotes: 0

Views: 287

Answers (1)

Abizern
Abizern

Reputation: 150565

You should call tableViewMain.reloadData() to get the table to reload itself.

Upvotes: 1

Related Questions