Search code examples
swiftalamofire

Dealing with the delay due to async communication in AlamoFire


I understand that AlamoFire and URL communications with a cloud database is async. My question is how do most people handle the couple second delay of communications. My app stores the users email address and a bool value of true if they are logged in. If true it jumps to the second viewcontroller with detailed info. Because of the delay there is no info. I put a 2 second delay but it looks kludgy on the transitions from view one to two. Attached is the code. I'm also thinking a wait screen but not sure. Any help would be great. Thanks.

var defaultValue:UserDefaults!

override func viewDidLoad() {
    super.viewDidLoad()

    defaultValue = UserDefaults.standard
    if let emailUD = defaultValue.object(forKey: "loginEmail") as? String{
        textEmail.text = emailUD
        print("Stored email -", emailUD)
    }
    if let loggedin = defaultValue.object(forKey: "isLoggedIn") as? Bool{
        if loggedin != true{
                let parameters1: Parameters=[
                    "email":"[email protected]",
                    "password":passwordIn
                ]
                Alamofire.request(URL_USER_LOGIN, method: .get, parameters: parameters1).responseJSON
                    { response1 in
                        if response1.result.value != nil{
                            let guardianJSON = JSON(response1.result.value!)
                            let guardian:String = guardianJSON[0]["guardian"].string!
                            print(guardian)
                            print(guardianJSON)

                            let league:String = guardianJSON[0]["league"].string!
                            let parameters2: Parameters=[
                                "league":league
                            ]
                            //Getting League name
                            Alamofire.request(URL_LEAGUE, method: .get, parameters: parameters2).responseJSON
                                { response2 in
                                    if response2.result.value != nil {
                                        let leagueJSON = JSON(response2.result.value!)
                                        leagueName=leagueJSON[0]["name"].string!
                                        print(leagueJSON)
                                    }
                            }
                        }
                }
            DispatchQueue.main.asyncAfter(deadline: .now() + 2.0){
                let loggedInVC = self.storyboard?.instantiateViewController(withIdentifier: "LoggedInID") as! LoggedINViewController
                self.present(loggedInVC, animated: false, completion: nil)
                print("They are logged in viewDidLoad")
            }
        }
    }
}

Solution

  • This

    DispatchQueue.main.asyncAfter(deadline: .now() + 2.0){
      let loggedInVC = self.storyboard?.instantiateViewController(withIdentifier: "LoggedInID") as! LoggedINViewController
      self.present(loggedInVC, animated: false, completion: nil)
      print("They are logged in viewDidLoad")
    }
    

    isn't the accurate way to go , as the time of 2 seconds may pass and the request still not returned or return before it and this will be a delay in your app which isn't a good practice , you need to create a function and call it from the callback like

    Alamofire.request(URL_LEAGUE, method: .get, parameters: parameters2).responseJSON   { response2 in
    
      if response2.result.value != nil {
        let leagueJSON = JSON(response2.result.value!)
        leagueName=leagueJSON[0]["name"].string!
        print(leagueJSON)
    
         // call here
        self.next()
      }
    }
    
    
    func next() {
       let loggedInVC = self.storyboard?.instantiateViewController(withIdentifier: "LoggedInID") as! LoggedINViewController
       self.present(loggedInVC, animated: false, completion: nil)
       print("They are logged in viewDidLoad")
    }