Reputation: 5644
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!
Upvotes: 93
Views: 100845
Reputation: 400
Assign timeoutIntervalForRequest & timeoutIntervalForResource for URLSession configuration its for full session including receiving Data these only controls the timeout between chunks of response Data.
private lazy var sessionManager: URLSession = {
let configuration = URLSessionConfiguration.default
configuration.waitsForConnectivity = true
configuration.timeoutIntervalForRequest = 60 // seconds
configuration.timeoutIntervalForResource = 60 // seconds
return URLSession(configuration: configuration)
}()
This wont give timeout error after exact 2 minute. To fix it also need to assign timeoutInterval to request:
URLRequest.timeoutInterval = 120 // sec
This fixed my problem of taking random amount aprox half an hour before return timeout error.
thanks for details
Upvotes: 0
Reputation: 405
In my case I was increasing the timeout of the wrong class. My timeout error was solved by increasing the timeout of the URLRequest not the URLSession
var request = URLRequest(url: url)
request.timeoutInterval = 30
Upvotes: 17
Reputation:
For Swift 4:
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = TimeInterval(15)
config.timeoutIntervalForResource = TimeInterval(15)
let urlSession = URLSession(configuration: config)
The timeoutIntervalForRequest is for all tasks within sessions based on this configuration. The timeoutIntervalForResource is for all tasks within sessions based on this configuration.
Upvotes: 3
Reputation: 40038
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
Upvotes: 157
Reputation: 2307
In swift 3. timeout 15 seconds.
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = TimeInterval(15)
configuration.timeoutIntervalForResource = TimeInterval(15)
let session = URLSession(configuration: configuration)
Upvotes: 12
Reputation: 1712
In SWIFT 3.0, You need to use
if let cleanURLString = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed),
let theURL: Foundation.URL = Foundation.URL(string: cleanURLString) {
var task:URLSessionDataTask?
let urlconfig = URLSessionConfiguration.default
urlconfig.timeoutIntervalForRequest = 20
urlconfig.timeoutIntervalForResource = 60
let session = Foundation.URLSession(configuration: urlconfig, delegate: self, delegateQueue: OperationQueue.main)
let request = URLRequest(url: theURL)
request.httpMethod = "POST"
request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringCacheData
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
let paramString = YourParameterString
request.httpBody = paramString.data(using: String.Encoding.utf8)
task = session.dataTask(with: request) {
(data, response, error) in
// Do here
})
dataTask.resume()
}
Upvotes: 3
Reputation: 7273
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)
Upvotes: 20
Reputation: 21003
NSURLSessionConfiguration includes the property timeoutIntervalForRequest:
@property NSTimeInterval timeoutIntervalForRequest
to control the timeout interval. There is also timeoutIntervalForResource for the timeout after the request is initiated.
Upvotes: 4