I am getting an error time to time, and irritating error
Optional(Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=-4, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <BB6171AF-E4AF-49F2-ACEE-A2CDA67B825E>.<3>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <BB6171AF-E4AF-49F2-ACEE-A2CDA67B825E>.<3>",
"LocalDataPDTask <BB6171AF-E4AF-49F2-ACEE-A2CDA67B825E>.<3>",
"LocalDataTask <BB6171AF-E4AF-49F2-ACEE-A2CDA67B825E>.<3>"
), NSLocalizedDescription=The network connection was lost., _kCFStreamErrorDomainKey=4, NSErrorFailingURLStringKey=https://url, NSErrorFailingURLKey=https://url})
Why am I getting this error?
I have a method which handles api calls called retry() and depending on the status i need to refresh my token and then retry the same request again. So what I have done is check whether the response after excuting the initial api call is SOMECODE and then refresh the access token if it is calling the refreshAccessToken method and depending on its response if it's a success then I recursively call the same function retry() again and excute the logic. It works fine sometimes and sometimes it gives the above error
if responseDescDisplay == "SOMECODE"{
refreshAccessToken(){ isSuccess,isFailed, errCode in
if isSuccess {
retry(urlString: urlString, method: method, requestBody: requestBody, completionHandler: completionHandler)
retry = true;
}else{
if !retry {
if errCode.isEmpty {
completionHandler(nil,nil,"OHNO");
}else{
completionHandler(nil,nil,"ERROR");
}
}
}
My refresh access token method looks something like this,
URLSession.shared.dataTask(with: tokenRegenerateRequest) { data, res, err in
print("EROR: ", err)
print("DARA: ", data)
if err != nil {
print("IN HERE")
completionHandler(false, false, "INTERNAL_SERVER_ERROR");
}
guard let data = data, err == nil else {return}
guard let httpResponse = res as? HTTPURLResponse else {
return
}
// print("REGENRATING TOKEN \(httpResponse.statusCode) \(res)");
if httpResponse.statusCode == 200 {
if !data.isEmpty {
do {
let decodedResponse = try! JSONDecoder().decode(RefreshTokenResponse.self, from: data);
print("ACCESS TOKEN REGENRATE REQUEST: ", decodedResponse)
if !decodedResponse.accessToken.isEmpty && !decodedResponse.refreshToken.isEmpty {
completionHandler(true,false, "")
} else {
print("HERE1")
completionHandler(false,true,"")
}
}catch{
print("HERE2")
completionHandler(false,true,"")
}
} else {
print("HERE3")
completionHandler(false,true,"")
}
}else {
print("HERE4", httpResponse.statusCode)
completionHandler(false,true,"")
}
}.resume()
What am i doing wrong. Why do I keep getting the above error?