-1

I use the following function to fetch data from an api, however what I am struggling to do is timing out the function when the set time runs out.

My objective:

-I want the following code to wait for some time, e.g. 10 seconds and if 10 seconds have passed, I want it to continue with other functions and work i.e. not keep on waiting. For that I am using dispatchgroup wait method. However it is not working as intended. How do I adjust the following to achieve that?.


class ViewController: UIViewController {

    var myArr = [testing]()
    let dispatchGROUP=DispatchGroup() 
. . .}
    @objc func warmUp(url: String, u: String, p: String){
    

        let waitResult = dispatchGROUP.wait(timeout: .now() + 10)

        AF.request(url, method: .get)
            .authenticate(username: u, password: p)
            .response { [weak self] response in

                guard let safeData = response.data else {self?.sArr.append("0")/*; self?.warm.leave()*/; print("0 sent"); print(url,u,p); return}
                let xml = XML.parse(safeData)
                let sid = xml.Response.SID.text ?? ""
                print(sid)
                self?.sArr.append(sid)
//                self?.dispatchGROUP.leave()
                if waitResult == .success{
                    print("success")
                    self?.dispatchGROUP.leave()
                }else if waitResult == .timedOut{
                    print("fail")
                    self?.dispatchGROUP.leave()
                    return
                }
            }
    }
deathAdder
  • 13
  • 8
  • https://stackoverflow.com/a/61192412/1801544 ? – Larme Aug 05 '21 at 08:00
  • Why are you using Disptach Group for that? – El Tomato Aug 05 '21 at 08:10
  • Besides, where does a guy named 'dispatchGROUP' come from? – El Tomato Aug 05 '21 at 08:12
  • @ElTomato need this api call to finish before firing another one, 2nd call is dependent on this one. Declared in class, under viewcontroller – deathAdder Aug 05 '21 at 08:13
  • "need this api call to finish before firing another one, 2nd call is dependent on this one" Then ask how to do that. Don't assume dispatch group and then ask why it doesn't work. "Why isn't this screwdriver driving in this nail?" If you asked how to drive in a nail, you could be told about a hammer. – matt Aug 05 '21 at 10:32
  • Try setting the `timeoutInterval` parameter https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#setting-other-urlrequest-properties – Rob Aug 05 '21 at 19:47
  • FWIW, never `wait` on the main thread. It will deadlock the app (or in this case, deadlock until it expires). Use asynchronous patterns. – Rob Aug 05 '21 at 20:08
  • 1
    @Rob yes timeoutinterval works and achieves what i wanted, thankyou – deathAdder Aug 06 '21 at 08:32
  • You are forcing an asynchronous task to become synchronous. **Don't do that**. Learn to understand how asynchronous data processing works. – vadian Aug 06 '21 at 08:41

1 Answers1

1

stackoverflow.com/a/61192412/1801544 Please set timeout interval, I believe this is what you want