0

I'm working on an app that fetch data from a website. When the user hit the home button then open the app again (from background), I want to reload the data again to the viewController.

I tried the following code:

in app delegate:

class AppDelegate: UIResponder, UIApplicationDelegate {

  var myViewController: ViewController?
   ---------

  var myViewController: rootViewController?
        func applicationDidEnterBackground(application: UIApplication) {

        print("Goodbye world") //.... then whatever code after pressing the home button
}

func applicationWillEnterForeground(application: UIApplication) {

    print("Hello World")
    myViewController.ObtianData() // which is pretty much the func in my app that fetch data from the web and display it in tableView

   }

Then in the ViewController under ViewDidLoad

 override func viewDidLoad() {  
   // I added the print to log here to check if the viewDidLoad function is being called but apparently it is not. 
   print ("Hello again from ViewController")

  let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate! as! AppDelegate
    appDelegate.myViewController? = self 
}

Any suggestions?

abha
  • 73
  • 7

3 Answers3

2

The root cause of the problem that you are having is that the data that you load is attached to the view controller that displays it. This goes against the MVC principle, which suggests that the model needs to be separated from the controller.

You should reorganize the classes in such a way that ObtainData is split between the model and the controller:

  • The model goes out and obtains the data,
  • The controller decides what to do with the data.

Make a class called Model (or pick some other name with Model in it) and store the data for your table in it. Make a single instance of that class statically accessible from everywhere through Model.instance (i.e. implement a Singleton in Swift).

Change your view controller to rely on Model.instance for its data, rather than storing it internally.

That is all you need to do to separate the pieces of your app. Now your problem can be solved in exactly two lines of code - applicationWillEnterForeground should call Model.instance.obtainData, and your controller's viewWillAppear should call reloadData on its tableView.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

You should use NSNotificationCenter event UIApplicationDidBecomeActiveNotification, it was made especially for that.

(You don't need to use AppDelegate)

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: @selector(applicationDidBecomeActive),
        name: UIApplicationDidBecomeActiveNotification,
        object: nil)
}

func obtianData() {
    // do something
}

Note that swift standard require function name to start with a lower case.

Matan Lachmish
  • 1,235
  • 12
  • 17
0

add Notification with UIApplicationDidEnterBackgroundNotification and UIApplicationDidEnterBackgroundNotification