处理由于AlamoFire中的异步通信导致的延迟

时间:2019-06-20 21:56:34

标签: swift alamofire

我了解AlamoFire和URL与云数据库的通信是异步的。我的问题是大多数人如何处理几秒钟的通信延迟。我的应用程序存储了用户的电子邮件地址,如果他们已登录,则将布尔值设置为true。如果为true,它将跳转到包含详细信息的第二个viewcontroller。由于延迟,因此没有信息。我将延迟时间设置为2秒,但从视图1到视图2的过渡看起来有些混乱。随附的代码。我也在考虑等待屏幕,但不确定。任何帮助都会很棒。谢谢。

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":"perry.aten@mymail.com",
                    "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")
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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")
}

并不是正确的方法,因为2秒的时间可能过去了,请求仍然没有返回或在此之前返回,这将延迟您的应用程序,这不是一个好习惯,您需要创建一个函数并从回调中调用它,如

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")
}