4

Im building an IoT app using Arduino board and calling an API to check the updated pin values of the Arduino board.

When i receive the update from the API i have to update my button colors to red or green according to the data i'm receiving. First time i load the app it works just fine and the viewDidAppear() is called. But when i go to the background then to the foreground again it is not called. What i know is that every time view appears it has to run the instructions inside of the function but seems like that is not the case.

I tried to put my code in the AppDelagate applicationDidBecomeActive() but because i'm trying to update my views and the view is not there yet it gives me fatal error found nil. Here are my instructions in the viewDidAppear()

    override func viewDidAppear(_ animated: Bool) {

    activityIndicator.startAnimating()

    definingPinModes(pin: [7,6,5,4], mode: 1)

    getAllInputValues(key: key, method: .post) { (status, newValues, msg) in

        if status == 200 {
            //Change button colors according to values in array
            self.changeButtonColors(values: newValues!)

        } else {

            print("Error getting pin values")

            self.alertMessage(title: "Connection Error", message: "Failed retrieving pin values")
            return
        }
        self.activityIndicator.stopAnimating()
    }

}
Kegham K.
  • 1,449
  • 15
  • 31

1 Answers1

3

In addition to viewDidAppear, you can have your view controller observe UIApplication.didBecomeActiveNotification (previously known as UIApplicationDidBecomeActive):

private var observer: NSObjectProtocol?

override func viewDidLoad() {
    super.viewDidLoad()

    observer = NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: .main) { [weak self] notification in
        self?.updateUI()
    }
}

deinit {
    if let observer = observer {
        NotificationCenter.default.removeObserver(observer)
    }
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    updateUI()
}

private func updateUI() {
    // do your UI update stuff here
}
Rob
  • 371,891
  • 67
  • 713
  • 902
  • Thanks Rob first time to use these observers. But i had a question regarding creating an instance of the NotificationCenter itself. I did it without creating the instance and the need to call deinit . Why is your way better than using the NotificationCenter class directly? – Kegham K. Dec 13 '16 at 23:02
  • I'm not creating a `NotificationCenter` instance. I'm just using the `default` singleton. All I'm doing is adding an observer to that notification center and saving a reference to that observer (not the center) so that I can remove that observer in `deinit`. In terms of why you do that, historically it has been critical that when an object is deallocated that it inform the notification center to stop sending it notifications. Now perhaps this is a root view controller that won't get deallocated, but it's simply good practice to clean up after yourself so the code can be used in any context. – Rob Dec 14 '16 at 00:23
  • See the [`addObserver(forName:object:queue:using:)`](https://developer.apple.com/reference/foundation/notificationcenter/1411723-addobserver) documentation for example of adding observers and removing them. – Rob Dec 14 '16 at 00:32