I am using iOS 7's new NSURLSessionDataTask
to retrieve data as follows:
NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest: request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) { // }];
How can I increase the time out values to avoid the error "The request timed out"
(in NSURLErrorDomain
Code=-1001
)?
I have checked the documentation for NSURLSessionConfiguration but did not find a way to set the time out value.
Thank you for your help!
The request timeout interval controls how long (in seconds) a task should wait for additional data to arrive before giving up. The timer associated with this value is reset whenever new data arrives. When the request timer reaches the specified interval without receiving any new data, it triggers a timeout.
“settimeout in swift” Code AnswerDispatchQueue. main. asyncAfter(deadline: . now() + 2.0) { // Change `2.0` to the desired number of seconds.
URLSession is one class of the URLSession API. There are a handful of types you need to become familiar with to perform an HTTP request in Swift. A URLSession instance is the manager or coordinator of the requests your application performs. A request is referred to as a task, an instance of the URLSessionTask .
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; sessionConfig.timeoutIntervalForRequest = 30.0; sessionConfig.timeoutIntervalForResource = 60.0;
let sessionConfig = URLSessionConfiguration.default sessionConfig.timeoutIntervalForRequest = 30.0 sessionConfig.timeoutIntervalForResource = 60.0 let session = URLSession(configuration: sessionConfig)
timeoutIntervalForRequest
and timeoutIntervalForResource
specify the timeout interval for the request as well as the resource.
timeoutIntervalForRequest
- The timeout interval to use when waiting for additional data. The timer associated with this value is reset whenever new data arrives. When the request timer reaches the specified interval without receiving any new data, it triggers a timeout.
timeoutIntervalForResource
- The maximum amount of time that a resource request should be allowed to take. This value controls how long to wait for an entire resource to transfer before giving up. The resource timer starts when the request is initiated and counts until either the request completes or this timeout interval is reached, whichever comes first.
Based on NSURLSessionConfiguration Class Reference
In case Swift developer coming here
to do this, you need to use
let urlconfig = NSURLSessionConfiguration.defaultSessionConfiguration() urlconfig.timeoutIntervalForRequest = 12 urlconfig.timeoutIntervalForResource = 12 self.session = NSURLSession(configuration: urlconfig, delegate: self.delegates, delegateQueue: nil)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With