I am new to Swift and currently stuck on exiting a function containing shared.dataTask
once the httpResponse is 200. Any suggestions will be appreciated.
func retryFunc(url: String, requestType: String, requestJsonData: Any, retriesLeft: Int) {
//Function body
// declarations
while self.numberOfretries > 0 {
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
if let httpResponse = response as? HTTPURLResponse {
print(httpResponse.statusCode)
if httpResponse.statusCode == 200 {
print("Got 200")
self.numberOfretries = 0
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
print(responseJSON)
}
return
}
if httpResponse.statusCode == 500 {
print("Got 500")
self.numberOfretries -= 1
self.retryFunc(url: url, requestType: <request-type>, requestJsonData: json, retriesLeft: self.numberOfretries)
}
}
}
task.resume()
}
}
//calling function from another class
func retryFunc(url: <url>, requestType: <type>, requestJsonData: <jsonData>, retriesLeft: <3>)
I need to exit the function on getting 200 but it still continues to run for the number of retries specified while calling the function.