1

I have a function where I make a request to get a JSON.

let request = NSMutableURLRequest(URL: NSURL(string: path)!)
let session = NSURLSession.sharedSession()

session.configuration.timeoutIntervalForRequest = 5
session.configuration.timeoutIntervalForResource = 5

let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
     let json:JSON = JSON(data: data!)
     onCompletion(json, error)
})
task.resume()

This works, but I´m having issues with the timeout. If the request takes more than 5 seconds I want to cancel the operation. Any ideas how to do this?

The call can take more than 5 seconds but it stills fires and the timeout does not do anything. Do I have to do anything if the timeout is fired?

Update

        let request = NSMutableURLRequest(URL: NSURL(string: path)!)

        let urlconfig = NSURLSessionConfiguration.defaultSessionConfiguration()
        urlconfig.timeoutIntervalForRequest = 5
        urlconfig.timeoutIntervalForResource = 5
        let session = NSURLSession(configuration: urlconfig, delegate: self, delegateQueue: nil)

        let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
            let json:JSON = JSON(data: data!)
            onCompletion(json, error)
        })
        task.resume()
  • you already are using the time out interval request for 5 sec. What else do u need ? – Teja Nandamuri Jan 03 '16 at 21:22
  • This is not working... I tried to change that value to 0.01 and it still loaded when it took over 4 seconds to load. Do I have to handle if the timeoutIntervalForRequest or timeoutIntervalForResource is fired? –  Jan 03 '16 at 21:23
  • @Mr.T, it´s not a duplicate I have read that post and check my comment above. –  Jan 03 '16 at 21:24

1 Answers1

3

You cannot change the timeout on the sharedSession or a sessions configuration property. You have to set the timeout when the session is created. See this for an example of how to create a session and its timeout: https://stackoverflow.com/a/30427187/78496

Community
  • 1
  • 1
chedabob
  • 5,835
  • 2
  • 24
  • 44
  • I don´t quite get how to integrate that post with my code, any ideas? –  Jan 03 '16 at 21:29
  • 3
    @user5700760 - Replace your `let session = ...` line with the four lines from that answer (though rather than referring to `self.session`, you could just refer to `session`). The key point is to instantiate your own `NSURLSessionConfiguration`, set its timeout parameters, and then instantiate your own `NSURLSession` using that configuration rather than using the shared session. – Rob Jan 03 '16 at 21:33
  • I have now updated my code (see the update in my question), but now when the code reaches the timeout it crashes at `let json:JSON = JSON(data: data!)` with the message: fatal error: unexpectedly found nil while unwrapping an Optional value –  Jan 03 '16 at 21:39
  • If it timed-out then `data` will be nil as it didn't receive any. You have to check if `error` is not nil before you do anything with `data` – chedabob Jan 03 '16 at 21:40
  • Thanks chedabob and Rob, appreciate your help. –  Jan 03 '16 at 21:43