Refresh NSFetchedResultsController data?

前端 未结 4 750
暗喜
暗喜 2021-02-07 11:48

I can create new managed objects inside my app, in a separate view I have a table view which shows all the objects I have.

When I create a new managed object (in a total

4条回答
  •  情歌与酒
    2021-02-07 12:38

    In case someone having the same issue, as I just had it. I have tried the above solution with green tick, but it wouldn't work for me. I have followed Apple's code and everything went fine.

    simply, just implement the three "Fetchcontroller" delegate functions

        func controllerWillChangeContent(controller: NSFetchedResultsController) {
                self.tableView.beginUpdates()
            }
    
    
       func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    
                switch type {
                case .Insert:
                    self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
                case .Delete:
                    self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
                case .Update:
                    print("")
                    self.configureCell(self.tableView.cellForRowAtIndexPath(indexPath!)!, indexPath: indexPath!)
                case .Move:
                    self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
                    self.tableView.insertRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
                }
            }
    
    
     func controllerDidChangeContent(controller: NSFetchedResultsController) {
                self.tableView.endUpdates()
            }
    

提交回复
热议问题