mvasco
mvasco

Reputation: 5101

viewWillAppear not always executed when viewController is present

I am working on an iOS app.

There is a viewController A that perfoms a segue programmatically to a detail viewController D.

On that detail viewController D the user may change some information that should be updated when clicking the back button to viewController A.

On viewController A I have included this function:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    determineMyCurrentLocation()
    print("reloading data")

        downloadJSONFavoritos {
            print("reloading viewwillappear")
        
        self.collectionViewFavoritos.reloadData()
    }
}

My issue is that this function is not always called when back to viewController A, and then the information in A is not updated as it should.

Upvotes: 0

Views: 177

Answers (1)

vpoltave
vpoltave

Reputation: 1770

I will create simple example, please adaptate it to your needs.

ViewController A:

class ViewControllerA: UIViewController {

    func someMethod() {
        let viewControllerD = // creation and displaying logic of your viewController D
        viewControllerD.updateCompletionHandler = { [unowned self] in 
            /// Any updates goes here ...
        }
    }
}

ViewController D:

class ViewControllerD: UIViewController {

   var updateCompletionHandler: (() -> Void)?

   /// Just example
   @IBAction func triggerUpdateButton(_ sender: UIButton) {
       /// Call it, in place where user make changes
       updateCompletionHandler?()   
   }
}

If you have any questions, let me know in the comments.

Upvotes: 2

Related Questions