与alamofire排队

问题描述 投票:1回答:1

当我使用Alamofire时,我遇到了执行任务的问题我使用了两次Alamofire,第一次收集数据(令牌)然后我会用它来发送我的Post请求。

我的两个请求之间的问题,数据的恢复是在第二个请求之后完成的。

import Foundation
import Alamofire
import SwiftyJSON

class Helper {
    func alomofireGet(URL: String) -> JSON {
        let queue = DispatchQueue(label: "com.test.com", qos: .background, attributes: .concurrent)
        var contenuJSON = JSON()
        Alamofire.request(URL, method: .get).responseJSON(queue: queue) { (reponse) in
            if reponse.result.isSuccess {
                contenuJSON = JSON(reponse.result.value!)
                print(contenuJSON)
            }
            else {
                contenuJSON = JSON(reponse.result.error!)
            }
        }
        return contenuJSON
    }
    func alomofirePost(URL: String, Paramaters: Dictionary<String, Any>) -> JSON {
        var contenuJSON = JSON()
        Alamofire.request(URL, method: .post, parameters: Paramaters, encoding: JSONEncoding.default).responseJSON { (reponse) in
            if reponse.result.isSuccess {
                contenuJSON = JSON(reponse.result.value!)
            }
            else {
                contenuJSON = JSON(reponse.result.error!)
            }
        }
        return contenuJSON
    }
}

在新文件=与内容令牌的差异

let request = Helper()
@IBOutlet weak var emailText: UITextField!
@IBOutlet weak var passwordText: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()
    self.hideKeyboardWhenTappedAround()
}
@IBAction func login(_ sender: Any) {
    let contenuJSON = request.alomofireGet(URL: "http://192.168.1.7/app_dev.php/login/app")
    print(contenuJSON)
    let token = contenuJSON["csrfToken"].stringValue
    print(token) // /\ EMPTY
    let Paramaters = ["_csrf_token": token, "_password": self.passwordText.text!, "_redirect_url": "", "t_path": "", "_username": self.emailText.text!]
    let contenuRequest = request.alomofirePost(URL: "http://192.168.1.7/app_dev.php/login_check", Paramaters: Paramaters)
    print(token) // /\ FULL /\
}

}

swift alamofire
1个回答
0
投票

对Alamofire的API调用是异步过程,因此你的alamofireGetalamofirePost返回刚刚初始化的json对象 - JSON(),它没有任何数据。

解:

您应该使用@escaping closure,它将保留控件,直到您从第一次API调用获得结果。

func alomofireGet(URL: String, onCompletion:((JSON) -> Void)) {
    let queue = DispatchQueue(label: "com.test.com", qos: .background, attributes: .concurrent)
    var contentJSON = JSON()
    Alamofire.request(URL, method: .get).responseJSON(queue: queue) { (reponse) in
        // Load contentJSON with data
        if reponse.result.isSuccess {
            contenuJSON = JSON(reponse.result.value!)
        } else {
            contenuJSON = JSON(reponse.result.error!)
        }
        // Send contentJSON via `onCompletion` block
        onCompletion(contenuJSON)
    }
}

func alomofirePost(URL: String, Paramaters: Dictionary<String, Any>, onCompletion: @escaping ((_ response: JSON) -> Void)) {
    var contenuJSON = JSON()
    Alamofire.request(URL, method: .post, parameters: Paramaters, encoding: JSONEncoding.default).responseJSON { (reponse) in
        // Load contentJSON with data
        if reponse.result.isSuccess {
            contenuJSON = JSON(reponse.result.value!)
        } else {
            contenuJSON = JSON(reponse.result.error!)
        }
        // Send contentJSON via `onCompletion` block
        onCompletion(contenuJSON)
    }
}

在视图控制器中将其命名为:

    let usernameStr = self.emailText.text!
    let passwordStr = self.passwordText.text! 

    Helper().alomofireGet(URL: "http://192.168.1.7/app_dev.php/login/app") { contenuJSON in
        print(contenuJSON)

        DispatchQueue.main.async {
            let token = contenuJSON["csrfToken"].stringValue
            print(token)

            let Paramaters = ["_csrf_token": token, "_password": passwordStr, "_redirect_url": "", "t_path": "", "_username": usernameStr]
            Helper().alomofirePost(URL: "http://192.168.1.7/app_dev.php/login_check", Paramaters: Paramaters) { contenuJSON in
                print(token)
            }
        }

    }
© www.soinside.com 2019 - 2024. All rights reserved.