Brosef Brosef - 5 months ago 18
iOS Question

Redundant tableview after fetch request

I have a

UITableView
and the cells display data that is fetched from my backend. I can navigate to different view controllers from the view controller that contains my tableview. When I segue back to my view controller with the tableview, I want to update the table with an updated fetch request.

As of now I have:

override func viewDidLoad() {
self.fetchData()
self.dataTable.reloadData()
}

override func viewWillAppear(animated: Bool)
{

self.fetchData()
self.dataTable.reloadData()

}


This just causes the tableview to be redundant and display the same data (obviously). What would be the correct way to update the table without displaying the same information? Should I clear the array from the prior fetch request before each fetch request?

Answer

You have to remember that viewDidLoad() is only called once during navigation segues. So, after it's loaded, the only time you'll know that the view has popped up again is in the viewWillAppear().

Have you tried:

override func viewWillAppear(animated: Bool)
{
    self.clearData()
    //Once you've reset all your variables, populate them again. 
    self.fetchData()
    self.audioData.reloadData()
    self.dataTable.reloadData()

} 

If I'm wrong, and your viewDidLoad() method is being called every time the view appears, then your code should work. You can always put print statements in each function to see what's being called, and what isn't. Let me know if this helps :)

Comments