简体   繁体   English

网络速度慢或服务器宕机时如何处理Alamofire请求?

[英]how to handle Alamofire request when network speed is slow or server is down?

I am new to the Alamofire which I am using to make a request to rest api. 我是Alamofire ,正在使用它来请求休息api。 Now while making request there can be two issues and I want to know how to handle those issues using Alamofire . 现在,在发出请求时可能会有两个问题,我想知道如何使用Alamofire处理这些问题。

1) What if user have submit data and Internet is slow and it takes longer to get response from server. 1)如果用户提交了数据并且Internet速度很慢并且需要更长的时间才能从服务器获得响应,该怎么办。 In such case how one should insure that whether a request is successful or not. 在这种情况下,应如何确保请求是否成功。 Or we can show some message to user that Internet is slow so he can wait for long response. 或者,我们可以向用户显示一些消息,指出互联网速度较慢,因此他可以等待较长的响应时间。

2) What if internet speed is ok but the server is down or it is taking longer to send a response how should we handle these situations in our application. 2)如果互联网速度可以,但是服务器已关闭或者发送响应的时间更长,该如何在应用程序中处理这些情况呢? And maintain integrity of data. 并保持数据的完整性。

Following is an example of how I am using Alamofire to make request. 以下是我如何使用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)
        }
    }
}

You're actually implementing alamofire correctly I would already be about the connection and server problem if network speed is slow. 您实际上是在正确实现alamofire,如果网络速度很慢,我早会遇到连接和服务器问题。 Try using a different endpoint from a different server to test the speed.If its faster then the problem is on your server. 尝试使用其他服务器上的其他端点来测试速度。如果速度更快,则问题出在服务器上。 Since your implementation is correct then definitely it is server problem. 由于您的实现是正确的,那么肯定是服务器问题。 alamofire has nothing to do on handling the issue if the problem is on the server or connection. 如果问题出在服务器或连接上,alamofire与该问题无关。

You can increase timeout for slow response API calls. 您可以增加响应缓慢的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)
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM