简体   繁体   中英

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. Now while making request there can be two issues and I want to know how to handle those issues using Alamofire .

1) What if user have submit data and Internet is slow and it takes longer to get response from server. 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. And maintain integrity of data.

Following is an example of how I am using Alamofire to make request.

 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. 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.

You can increase timeout for slow response API calls.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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