Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift ios - How to run function in ViewController from AppDelegate

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!

like image 327
Victor Avatar asked May 07 '17 12:05

Victor


People also ask

What is the difference between AppDelegate and SceneDelegate in Swift?

AppDelegate is responsible for handling application-level events, like app launch and the SceneDelegate is responsible for scene lifecycle events like scene creation, destruction and state restoration of a UISceneSession.

What is use of AppDelegate in Swift?

The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system. Like the UIApplication object, UIKit creates your app delegate object early in your app's launch cycle so it's always present.

What is @main in AppDelegate?

UIApplicationMain first instantiates UIApplication and retains its instance to serve as the shared application instance ( UIApplication. shared ) and then instantiates the app delegate marked @Main as the application instance's delegate. The main method exists as a type method.


2 Answers

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)
} 
like image 182
Scriptable Avatar answered Oct 22 '22 17:10

Scriptable


I simply solved it like this:

func applicationDidBecomeActive(_ application: UIApplication) {
        let viewController = self.window?.rootViewController as! ViewController
        viewController.grabData()
}
like image 20
Victor Avatar answered Oct 22 '22 15:10

Victor