【问题标题】:how to handle Alamofire request when network speed is slow or server is down?网速慢或服务器宕机时如何处理 Alamofire 请求?
【发布时间】:2018-10-02 06:05:15
【问题描述】:

我是 Alamofire 的新手,我用它来请求休息 api。 现在在提出请求时可能会出现两个问题,我想知道如何使用Alamofire 处理这些问题。

1) 如果用户有提交数据并且互联网很慢并且需要更长的时间从服务器获得响应怎么办。在这种情况下,应如何确保请求是否成功。或者我们可以向用户显示一些消息,即互联网速度很慢,以便他等待较长的响应。

2) 如果网速正常但服务器宕机或发送响应需要更长的时间怎么办?我们应该如何在应用程序中处理这些情况。并保持数据的完整性。

以下是我如何使用Alamofire 发出请求的示例。

 static func getUserListFromServer(completion: @escaping(Bool,[Users]?,String?)->() ){

    Alamofire.request(APPURL.getFeedbackUserList, method: .get, parameters: nil, encoding: URLEncoding.queryString, headers: nil).responseJSON { (responseJson) in

        responseJson.result.ifSuccess {
            do {
                // Decode data to object
                let jsonDecoder = JSONDecoder()
                let root = try jsonDecoder.decode(Root.self, from: responseJson.data!)
                if let users = root.users{
                        completion(true,users,nil)
                }else{
                        completion(false,nil,"No users found.")
                }
            }
            catch let err {
                print(err.localizedDescription)
                completion(false,nil,err.localizedDescription)
            }
        }
        responseJson.result.ifFailure {
                    completion(false,nil,responseJson.result.error?.localizedDescription)
        }
    }
}

【问题讨论】:

标签: ios swift alamofire response internet-connection


【解决方案1】:

你实际上是正确地实现了 alamofire 如果网络速度很慢,我已经知道连接和服务器问题了。尝试使用来自不同服务器的不同端点来测试速度。如果速度更快,则问题出在您的服务器上。既然你的实现是正确的,那么肯定是服务器问题。如果问题出在服务器或连接上,alamofire 与处理问题无关。

【讨论】:

  • 这个建议不是很好的 IMO。第一步应该是使用不同的客户端(cURL、Postman 等)测试服务器。当您使用库进行联网时,会为您做出很多决定,并且可能难以调试。要考虑的一项设置是超时/重试策略。
【解决方案2】:

您可以增加响应缓慢的 API 调用的超时时间。

static func getUserListFromServer(completion: @escaping(Bool,[Users]?,String?)->() ){

let request = NSMutableURLRequest(url: APPURL.getFeedbackUserList)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.timeoutInterval = 10 // change time out according to your need
let values = ["key": "value"]
request.httpBody = try! JSONSerialization.data(withJSONObject: values, options: [])

Alamofire.request(request as! URLRequestConvertible).responseJSON { (responseJson) in
    responseJson.result.ifSuccess {
        do {
            // Decode data to object
            let jsonDecoder = JSONDecoder()
            let root = try jsonDecoder.decode(Root.self, from: responseJson.data!)
            if let users = root.users{
                    completion(true,users,nil)
            }else{
                    completion(false,nil,"No users found.")
            }
        }
        catch let err {
            print(err.localizedDescription)
            completion(false,nil,err.localizedDescription)
        }
    }
    responseJson.result.ifFailure {
                completion(false,nil,responseJson.result.error?.localizedDescription)
    }
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    相关资源
    最近更新 更多