执行segue时进行异步检查

时间:2018-10-04 07:41:07

标签: swift segue uistoryboardsegue moya

我的应用程序有两个ViewController,LoginViewController,MainViewController

在情节提要中,我创建了一个从LoginViewController到MainViewController的序列

现在我使用Moya来实现登录功能,但我想保持安全性

所以我在LoginViewController中编写了此功能

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    var ret = false
    let provider = MoyaProvider<MyApi>()
    provider.request(.login(username : inUsername.text! , password : inPassword.text! )) { result in
        switch result {
        case let .success(moyaResponse):
            let data = moyaResponse.data
            do {
                let decoder = JSONDecoder()
                let user = try decoder.decode(Login.self, from: data)
                if(user.status == 1){
                    ret = true
                }else{
                    print(user.msg)
                }
            }
            catch {
                print("error")
            }
        case let .failure(error):
            ret = false
        }
    }
    return ret
}

但是moya请求是异步的,此函数将在响应之前返回,因此该函数永远不会返回true

如何进行这项工作?


更新:

现在,我将请求移至IBAction按钮,但仍无法正常工作

如果我删除了shouldPerformSegue,它仍然会在回调之前转到下一个viewcontroller

如果我在shouldPerformSegue中返回true,则即使登录失败也会进入下一个viewcontroller

如果我在shouldPerformSegue中返回false,那么即使登录成功也不会转到下一个viewcontroller

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    return true // always to next 
    return false // never to next 
}

@IBAction func clickLogin(_ sender: Any) {

    let provider = MoyaProvider<ZfuApi>()
    provider.request(.login(username : inUsername.text! , password : inPassword.text! )) { result in
        switch result {
        case let .success(moyaResponse):
            let data = moyaResponse.data
            do {
                let decoder = JSONDecoder()
                let user = try decoder.decode(Login.self, from: data)
                if(user.status == 1){
                    self.performSegue(withIdentifier: "loginToMain", sender: sender)
                }else{
                    print(user.msg)
                }
            }
            catch {
                print("error")
            }
        case let .failure(error):
            print(error.response?.description)
        }
    }
}

2 个答案:

答案 0 :(得分:3)

您不能在shouldPerformSegue中使用异步方法,但是您可以在执行segue之前执行异步方法,然后在完成块中调用performSegue(withIdentifier:sender:)

答案 1 :(得分:1)

您可以引入一个属性来执行segue,如下所示

var isLoggedIn: Bool = false {
    didSet {
        self.performSegue(withIdentifier: "loginToMain", sender: self)
    }
}

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    return isLoggedIn
}

@IBAction func clickLogin(_ sender: Any) {

    let provider = MoyaProvider<ZfuApi>()
    provider.request(.login(username : inUsername.text! , password : inPassword.text! )) { result in
        switch result {
        case let .success(moyaResponse):
            let data = moyaResponse.data
            do {
                let decoder = JSONDecoder()
                let user = try decoder.decode(Login.self, from: data)
                if(user.status == 1){
                    self.isLoggedIn = true
                }else{
                    print(user.msg)
                }
            }
            catch {
                print("error")
            }
        case let .failure(error):
            print(error.response?.description)
        }
    }
}