gpt4 book ai didi

swift - 使用 alamofire 排队

转载 作者:行者123 更新时间:2023-11-28 07:40:39 27 4
gpt4 key购买 nike

我在使用 Alamofire 时遇到任务执行问题我使用了两次 Alamofire,第一次是收集数据( token ),然后我将使用它来发送我的 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
}
}

在新文件中 = 与内容 token 的差异

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 /\
}

最佳答案

对 Alamofire 的 API 调用是异步过程,因此您的 alamofireGetalamofirePost 只返回初始化的 json 对象 - JSON() 而不是有没有数据。

解决方案:

您应该使用@escaping 闭包,它将保持控制权,直到您获得第一次 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)
}
}

在您的 View Controller 中调用它:

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

}

关于swift - 使用 alamofire 排队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52363946/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com