7

I am trying to run a function in certain ViewController using AppDelegate

func applicationDidBecomeActive(_ application: UIApplication) {
        ViewController().grabData()
}

But somehow the function does not seem to run at all when the app has become active after entering the app from the background.

The function looks like this

func grabData() {
        self._DATASERVICE_GET_STATS(completion: { (int) -> () in
            if int == 0 {
                print("Nothing")
            } else {
                print(int)

                for (_, data) in self.userDataArray.enumerated() {
                    let number = Double(data["wage"]!)
                    let x = number!/3600
                    let z = Double(x * Double(int))
                    self.money += z
                    let y = Double(round(1000*self.money)/1000)

                    self.checkInButtonLabel.text = "\(y) KR"
                }

                self.startCounting()
                self.workingStatus = 1
            }
        })
    }

And uses this var

var money: Double = 0.000

What have I missed?

Thanks!

2
  • 1
    stackoverflow.com/a/34745677/2303865 Commented May 7, 2017 at 12:54
  • 1
    Is the certain view controller the initial view controller? If yes create a property in AppDelegate and assign the reference to that view controller to the property. If not – the other way round – in viewDidLoad() of the certain controller get the AppDelegate instance and assign the reference from there. Notification is most likely not needed. Commented May 7, 2017 at 12:54

2 Answers 2

14

ViewController().grabData() will create a new instance of the ViewController and call this function. Then.. as the view controller is not in use it will be garbage collected/removed from memory. You need to be calling this method on the actual view controller that is in use. Not a new instance of it.

The best option would be to listen for the UIApplicationDidBecomeActive notification that iOS provides.

NotificationCenter.default.addObserver(
    self,
    selector: #selector(grabData),
    name: NSNotification.Name.UIApplicationDidBecomeActive,
    object: nil)

make sure that you also remove the observer, this is usually done in a deinit method

deinit() {
    NotificationCenter.default.removeObserver(self)
} 
Sign up to request clarification or add additional context in comments.

2 Comments

When should I remove the observer?
deinit is usually the best place to remove it. this means that as the view controller is de-initialised (released from memory) then it removes itself as an observer. I used to do this in viewDidUnload but this has been deprecated now
3

I simply solved it like this:

func applicationDidBecomeActive(_ application: UIApplication) {
        let viewController = self.window?.rootViewController as! ViewController
        viewController.grabData()
}

2 Comments

Don't do this. You should use the method posted by Scriptable
If sometime you will need to do things like reload a tableview I thing that your app will crash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.