0

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.

TylerP
  • 9,600
  • 4
  • 39
  • 43
Ozone17
  • 138
  • 7
  • You never use the retriesLeft parameters. You just use self.numberOfretries. – Ptit Xav Jan 04 '21 at 17:15
  • I think this is not a good idea if there is no internet connection it will probably drain the device's battery without a reason. Better to use Reachability and retry it when the flag changes. – Leo Dabus Jan 05 '21 at 01:21
  • This is just the snippet. Skipped the declarations part @PtitXav – Ozone17 Jan 05 '21 at 10:43

1 Answers1

3

Consider that the while loop is outside of the asynchronous task, so a couple of 100 tasks can be created before the counter is being decremented.

A better way is to remove the while loop and check for 0 inside the closure – the retriesLeft parameter makes no sense – for example

func retryFunc(url: String, requestType: String, requestJsonData: Any) {
    //Function body
    // declarations
    
    let task = URLSession.shared.dataTask(with: request) { data, response, error in
                        
        if let error = error {
            print(error.localizedDescription)
            return
        }
                            
        if let httpResponse = response as? HTTPURLResponse {
           print(httpResponse.statusCode)
           if httpResponse.statusCode == 200 {
               print("Got 200")
               do {
                   if let responseJSON = try JSONSerialization.jsonObject(with: data!) as? [String: Any] {
                       print(responseJSON)
                   }
               } catch { print(error) }

           } else if httpResponse.statusCode == 500 {
               print("Got 500")
               self.numberOfretries -= 1
               if self.numberOfretries == 0 { return }
    
               self.retryFunc(url: url, requestType: requestType, requestJsonData: requestJsonData)
           }
       }
    }
    task.resume()
}
vadian
  • 274,689
  • 30
  • 353
  • 361