Alamofire请求发送了两次

时间:2019-09-04 20:45:17

标签: swift

我是alamofire请求,但发现请求已发送两次。是alamofire的错误吗?

我试图放置一个断点,但是没有两次被调用,并且没有两次或两次触发该功能的按钮。

这是我的代码

    ARSLineProgress.show()
    guard let url = URL(string: "https://laghmat.kbmediawebites.com/api/rest/account") else {return}
    let headers = ["X-Oc-Merchant-Id": MerchantId, "X-Oc-Session": K_Defaults.string(forKey: "sessionID"),"ACCEPT":"application/json"]
    let params = [
        "firstname": firstName,
        "lastname": lastName,
        "email": email,
        "telephone": phone
    ] as! [String : String]
    Alamofire.request(url, method: .put, parameters: params,encoding: JSONEncoding.default, headers: headers as! HTTPHeaders).responseJSON {
        response in
        switch response.result {
        case .success:
            print(response)
            guard let data = response.data else { return }
            do {
                let value = try JSONDecoder().decode(GeneralResponse.self, from: data)
                if value.success == 1 {
                    ARSLineProgress.showSuccess()
                    var user = GlobalHelpers.getUser()
                    user?.firstname = firstName
                    user?.lastname = lastName
                    user?.telephone = phone
                    user?.email = email
                    GlobalHelpers.setUser(user)
                    self.dismiss(animated: true, completion: nil)
                } else {
                    ARSLineProgress.showFail()
                    self.alert(msg: LanguageManger.localize(word: "serverError"))
                }
            } catch let error {
                ARSLineProgress.showFail()
                self.alert(msg: LanguageManger.localize(word: "serverError"))
                print(error)
            }

            break
        case .failure(let error):
            ARSLineProgress.showFail()
            self.alert(msg: LanguageManger.localize(word: "serverError"))
            print(error)
        }
    }

1 个答案:

答案 0 :(得分:0)

好吧,我建议您在发出请求时锁定视图中的所有按钮。

for viewW in view.subviews
{
   if let buttonV = viewW as? UIButton
   {
      buttonV.isUserInteractionEnabled = false
   }
}

显然,您必须在开始时锁定并在结束时解锁。

如果这行不通,请在该行中插入一个断点

switch response.result {

第一次,您会看到您没有状态代码,因为服务器尚未响应,然后按下一步,您将看到状态代码,因为服务器已经响应您了。

这并不意味着两次请求,我认为这是因为握手正在发生。