0

I would like to call reloadData method of TableView when i return in foreground. My TableView is built programmatically.

In FirstViewController i've the methods to populate the TableView and the function reloadListOfApps where i call reloadDatamethods.

class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, MFMailComposeViewControllerDelegate {     

    @IBOutlet weak var listOfApps: UITableView!

    override func viewDidLoad() {
            super.viewDidLoad()
            listOfApps.delegate = self
            listOfApps.dataSource = self
    }

    func reloadListOfApps() {
        listOfApps.reloadData()
    }    

// ================ TABLE DELEGATE/MANAGER ===================

let apps = ListAppClass.listApp()

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return apps!.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()

    let bundle = apps! [indexPath.row]
    let bundle_temp = String(describing: bundle)

    cell.textLabel?.text = bundle_temp
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("Current Cell!")

}
}

In AppDelegate i've

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    FirstViewController().reloadListOfApps()
}

When i run the app i've this error

Fatal error: Unexpectedly found nil while unwrapping an Optional value

Reading some questions i've also check the Outlets in Storyboard, and seems all ok.

Hooks correspond to FirstViewController

Where is the error?

strano
  • 171
  • 12
  • your error can be here `let bundle = apps! [indexPath.row]` or `apps!.count` check if apps is `nil`, anyway your `FirstViewController` instanciated in `applicationWillEnterForeground` its different from the other one in the navigation stack – Reinier Melian Jan 23 '18 at 18:32

1 Answers1

4

Try add oberver in your view controller

NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillEnterForeground),
name: .UIApplicationWillEnterForeground,
object: nil)

callback

@objc func applicationWillEnterForeground() {

     listOfApps.reloadData()

}

//// problem explaining

when you write this code in AppDelegate

  FirstViewController().reloadListOfApps()

this creates an instance on the fly whose all properties are nil as you didn't load the storybaord object or Xib file associated with that is not presented away from the currently active one , control goes to reloadListOfApps function and find that listOfApps you want to reload is nil so crash happens , the solution above is one way , another way is to use delegate or make listOfApps a shared object that can be referenced anywhere

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • With your fix (and great explaination) now app doesn't crash anymore, but there isn't a data reload. The `TableView` is the same. If i kill and restart the app, instead, the Table is updated. – strano Jan 23 '18 at 18:54
  • It's maybe that the pull failed , try adding dummy data in background and check – Shehata Gamal Jan 23 '18 at 19:22
  • 1
    Fixed. It populate the table using `apps` that provides the `Array` catching data from. But it was always the same because out of the scope. Now i've placed it in the right place! Tnx! – strano Jan 24 '18 at 08:30