Force reload TableView data from external function.

Hi,
Is it possible to force reload a tableview data? I tried tableView.reload, but this does not seem to work. A few key points:

1) I have a pickerView within a child view that only covers the bottom of the screen & the TableView VC is always visible, so using ViewWillAppear / ViewDidLoad does not work.
2) The data is contained within a model file, and all I am doing is updating the model data.
3) The issue appears to be that once I dismiss the child VC that I cannot figure out a way to "reload" the tableview cells.
4) Using Xcode 11 and Swift 5.2, and I am not using Storyboard.

Here's the code that I am using to go between the main & child views.

From the ViewController:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

addChild(pickerVC)
view.addSubview(pickerVC.view)
pickerVC.didMove(toParent: self)
setupChildConstraints()

}

From the PickerView / Child VC to update data and pass it back to the main VC:

@objc private func updateType() {
if selectedFrequency != "" {
let vc = ViewController()
vc.didTapAdd(type: selectedFrequency)
view.willMove(toWindow: nil)
view.removeFromSuperview()

}
}

And finally, where I am attempting to reload the tableview from the main VC:

extension ViewController: PDSelectFreqDelegate {
func didTapAdd(type: String) {
print(type)
DispatchQueue.main.async {
self.tableView.reloadData()
}

}
}

Any help would be appreciated, thanks.
Answered by junkpile in 619586022
The problem is you’re creating a brand new instance of ViewController, which is not part of the hierarchy and doesn’t have anything to do with stuff displayed on the screen. You call the method on that then throw it away. So of course it has no effect. You need to call the update method on the actual instance that is active, not a new one.

It looks like you’re half way there already; you appear to have a PDSelectFreqDelegate protocol defined, which is the right way to do it in my opinion. So you should just declare a property of that type on the child (var delegate: PDSelectFreqDelegate?), set the delegate when you create the child VC (pickerVC.delegate = self), then in the child call didTapAdd on self.delegate instead of vc.

Incidentally there is no need to dispatch anything to the main queue. There’s nothing going on in background threads here from what I can see.
Accepted Answer
The problem is you’re creating a brand new instance of ViewController, which is not part of the hierarchy and doesn’t have anything to do with stuff displayed on the screen. You call the method on that then throw it away. So of course it has no effect. You need to call the update method on the actual instance that is active, not a new one.

It looks like you’re half way there already; you appear to have a PDSelectFreqDelegate protocol defined, which is the right way to do it in my opinion. So you should just declare a property of that type on the child (var delegate: PDSelectFreqDelegate?), set the delegate when you create the child VC (pickerVC.delegate = self), then in the child call didTapAdd on self.delegate instead of vc.

Incidentally there is no need to dispatch anything to the main queue. There’s nothing going on in background threads here from what I can see.
Thanks, that it was it!
Force reload TableView data from external function.
 
 
Q