I have the following methods in my "ViewController" class to populate custom cells in a UITableView from a GET API call. This works fine when app first loads.
//ViewController
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
loadDataInUITableViewCell()
}
func loadDataInUITableViewCell() {
apiCentral.sharedInstance.fetchData() {
result in
guard result.error == nil else {
self.handleError(result.error!)
return
}
if let fetchedData = result.value {
self.data = fetchedData
}
self.messagesTableView.reloadData()
}
}
How do I refresh the data when the app comes to the foreground? Well, I added the following to AppDelegate to run the "loadDataInUITableViewCell" method so that the data reloads.
//AppDelegate
let mainViewController = ViewController()
func applicationWillEnterForeground(_ application: UIApplication) {
mainViewController.loadDataInUITableViewCell()
}
ISSUE: The above mechanism works fine however, I get the following error when the app comes to the foreground.
"fatal error: unexpectedly found nil while unwrapping an Optional value"
The issue seems to be happening at "self.messagesTableView.reloadData()" line of code. I'm using Xcode 8.3 and coding in Swift 3. Any guidance would be appreciated. Thank you.