AsyncTask in viewDidLoad

501 views Asked by At

I'm working with a async Task function in my viewDidLoad, by the returned response I'm changing the view (colors, text...), but I have "errors":

  • sometimes when I change view and I come back to the view, I don't get my view background in orange, but I get it in Green (the green is returned after the response of the asynctask function) so it's not normal when I come back to the view it have to reload it...

  • sometimes the view still in a orange background, but the function is already passed...

        super.viewDidLoad()
    
    self.lblUpdate.text = "Mise à jour des nouveaux paramètres en cours..."
    self.viewUpdate.backgroundColor = UIColor.orange
    self.hideUpdateConstraint.isActive = false
    self.updateTopConstraint.isActive = true
    self.updateBottomConstraint.isActive = true
    
    if Reachability.isConnectedToNetwork() == true {
    
        self.updateDown = true
    
        Reachability.checkUrl(urlString:Configuration.MyVariables.url, finished: { isSuccess in
    
    
            if isSuccess == true {
    
                ProcessingTasks.updateSimulationParams()
                self.lblUpdate.text = "Les nouveaux paramètres (taux, durée, montant...) ont été mis à jour."
                self.viewUpdate.backgroundColor = UIColor(red: 151/255, green: 232/255, blue: 84/255, alpha: 1.0)
                print("ok c bon")
    
                self.updateDown = false
    
    
            } else {
                self.updateDown = true
                self.lblUpdate.text = "Les nouveaux paramètres (taux, durée, montant...) n'ont pas pu être mis à jour car notre service est actuellement indisponible."
                self.viewUpdate.backgroundColor = UIColor.red
            }
        })
    } else {
        updateDown = true
        self.lblUpdate.text = "Les nouveaux paramètres  (taux, durée, montant...) n'ont pas pu être mis à jour car vous ne disposez pas d'une connexion internet."
        self.viewUpdate.backgroundColor = UIColor.red
    }
    
    self.hideUpdateConstraint.isActive = true
    self.updateTopConstraint.isActive = false
    self.updateBottomConstraint.isActive = false
    self.hideUpdateConstraint.constant = 20
    self.viewUpdate.isHidden = true
    
    
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(SimulationViewController.handleTap(_:))))
    
1

There are 1 answers

7
B.Tekkan On BEST ANSWER

Short suggestion. Try to override "viewDidAppear" function instead of "viewDidLoad". "viewDidAppear" should be triggered before view changes. "viewDidload" only triggered on views first load.

override func viewDidAppear() {      
    //Enter your code here       
}