1

I am using alamofire for api calling. The request call is going to success block every time but sometimes with the same request the call is going to failure block with error "The network connection was lost". As there is no problem with network connection, I am not able to find the solution for the particular issue. Here is my code-

let manager = Alamofire.SessionManager.default
        manager.session.configuration.timeoutIntervalForRequest = 120
        manager.request(url, method: .post, parameters: requestDictionary ?? [:], encoding: JSONEncoding.default).responseJSON(completionHandler: { (response) in
            switch response.result {

            case .success:
                KVNProgress.dismiss()
                let jsonString: String = String(data: response.data!, encoding: String.Encoding.utf8)!
                print(jsonString)
                let dict : [String : Any ] = response.result.value! as! [String : Any]
                let msg = dict["Message"] as? String
                if msg == "Session id is not valid." {

                    //logout process…   

                    }, onCancelClick: {

                    })

                }else{
                    success(response.result.value! as AnyObject)
                }

            case .failure(let error):
                KVNProgress.dismiss()
                print("Request failed with error: \(error.localizedDescription)")
                failure(“Unexpected error”)
            }
        })

Thanks in advance. :)

My code is inside block where i have already checked for internet connection-

 if(Utilities.checkInternetConnection())
   {
        // My previous code
    }

    else
    {
        connectionFailed(Constant.serverAPI.errorMessages.kNoInternetConnectionMessage)
    }
adm v
  • 81
  • 9

1 Answers1

0

i too had this problem, many time API call return connection fail error.

i solve it it by catching the connection Fail error and recalling the API error one more time from connection fail closure

Ajay saini
  • 2,352
  • 1
  • 11
  • 25