ObjC
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.timeoutIntervalForRequest = 30.0;
sessionConfig.timeoutIntervalForResource = 60.0;
Swift
let sessionConfig = URLSessionConfiguration.default
sessionConfig.timeoutIntervalForRequest = 30.0
sessionConfig.timeoutIntervalForResource = 60.0
let session = URLSession(configuration: sessionConfig)
What docs say
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
NSOperationQueue
withmaxConcurrentOperationCount
). This is not too complicated if you're using the non-delegate based rendition ofNSURLSession
and are using the completion block renditions. Because of significant annoyances with theNSURLSession
architecture, it's a bit of a pain to do this right if using the delegate-based approach. – FacturesharedSession
? – SeclusiondefaultSessionConfiguration
and set thetimeOutIntervalForRequest
appropriate for your server. It's just that the timeout problems resulting from issuing more than four or five concurrent requests is a completely different problem and suggests a different solution. But if you're only issuing one or two requests and they're still timing out, then setting thetimeoutInterval
properties is the right approach. – Facture