While an existing Transition or Presentation is occurring; the navigation stack will not be updated

Asked

Viewed 128 times

0

Boas, I am trying to implement an Indicator loading in my application with a small message, so that the user can see that information is being processed (in this case fetching the API through Alamofire), but after using this method:

        let alert = UIAlertController(title: nil, message: "A carregar...", preferredStyle: .alert)

        alert.view.tintColor = UIColor.black
        let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50)) as UIActivityIndicatorView
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.style = UIActivityIndicatorView.Style.gray
        loadingIndicator.startAnimating();

        alert.view.addSubview(loadingIndicator)
        present(alert, animated: true, completion: nil)

        AF.request(request).responseDecodable { (response: DataResponse<ModeloDados>) in

            loadingIndicator.stopAnimating()
            alert.dismiss(animated: true, completion: nil)

            switch response.result{
             case .success(_):
                if(response.response?.statusCode == 200){
                    self.performSegue(withIdentifier: "segueProcurar", sender: self)
                }
             case .failure(_):
                print("failure")
            }
            print("code: \(response.response?.statusCode)")
        }

I can only use once, since after passing to the second viewcontroller, trying to use again gives the following error:

pushViewController:Animated: called on while an existing Transition or Presentation is occurring; the navigation stack will not be updated.

Someone can help me?

Thank you in advance for any reply.

2 answers

0

I believe the problem is that you are displaying an Alert when you have the pushSegue. So you can’t make the transition, because there’s an alert on the screen as you try to make the second transition. Recoomendo that you make a Dismiss in your Alert, before trying to display another UIViewController

  • I think I’m already doing Dismiss with "Alert.Dismiss(Animated: true, Completion: nil)"

0

The dismiss is executed asynchronously, i.e. dismiss has not finished executing when the performSegue is called

For this it is possible to use the completion:, follows the example below of how your code would look

let alert = UIAlertController(title: nil, message: "A carregar...", preferredStyle: .alert)

alert.view.tintColor = UIColor.black
let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50)) as UIActivityIndicatorView
loadingIndicator.hidesWhenStopped = true
loadingIndicator.style = UIActivityIndicatorView.Style.gray
loadingIndicator.startAnimating();

alert.view.addSubview(loadingIndicator)
present(alert, animated: true, completion: nil)

AF.request(request).responseDecodable { (response: DataResponse<ModeloDados>) in

    loadingIndicator.stopAnimating()
    alert.dismiss(animated: true, completion: {

        switch response.result{
          case .success(_):
            if(response.response?.statusCode == 200){
                self.performSegue(withIdentifier: "segueProcurar", sender: self)
            }
          case .failure(_):
            print("failure")
        }
        print("code: \(response.response?.statusCode)")
    })
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.