Search code examples
swiftcore-datatableviewreloaddata

How to reload TableView in other View?


I have some CoreData base wich I'm used in my TableView.

When I'm tried to clear those base in other View I have a message in my console log.

CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)

for deleting CoreData Array I'm used this code

self.historyArray.removeAll()
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "History")
fetchRequest.returnsObjectsAsFaults = false

do {
    let results = try managedContext.executeFetchRequest(fetchRequest)

    for managedObject in results
    {
        let managedObjectData:NSManagedObject = managedObject as! NSManagedObject 
        managedContext.deleteObject(managedObjectData)
    }
} catch {
    print("Detele all data")
}

I know I need to reload TableView, but how can I do this in other View? ill tried this, but this code don't work.

var tableViewHistoryClass = HistoryView()
self.tableViewHistoryClass.tableView.reloadData()

Please help me to fix this message.


Solution

  • Thanks all for answers! I'm created new method, all my Clear CoreData function i added to my View in which i have TableView for showing all data from CoreData :P

    override func viewDidLoad() {
        super.viewDidLoad()
    
        NSNotificationCenter.defaultCenter().addObserver(self, selector:"clearCoreDataArray", name: "clearAllData", object: nil)
    
    }
    
    func clearCoreDataArray() {
    historyArray.removeAll()
            let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            let managedContext = appDelegate.managedObjectContext
            let fetchRequest = NSFetchRequest(entityName: "History")
            fetchRequest.returnsObjectsAsFaults = false
    
            do
            {
                let results = try managedContext.executeFetchRequest(fetchRequest)
                for managedObject in results
                {
                    let managedObjectData:NSManagedObject = managedObject as! NSManagedObject
                    managedContext.deleteObject(managedObjectData)
                }
            } catch {
                print("Detele all data")
            }
        self.tableView.reloadData()
    }
    

    and in View when I'm need to use this method i use this code

    NSNotificationCenter.defaultCenter().postNotificationName("clearAllData", object: self)
    

    now i don't have any CoreData warnings