10

Sometimes network request from my app gets stuck, And I get the following error after a while,

failed with error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out."UserInfo={NSUnderlyingError=0x1390937f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://apis.xyz.xyz.com, NSErrorFailingURLKey=https://apis.xyz.xyz.com, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}

Any request I make after this, The same sent request is sent out and it fails with same error. So, no new request goes out. This happens very rarely, but it does happen.

I am using NSURLSessionDataTask:dataTaskWithRequest:completionHandler to make a network calls.

Gaurav Gandhi
  • 3,041
  • 2
  • 27
  • 40
Shimin
  • 101
  • 1
  • 3
  • did you solve this problem?? Am getting this problem but can't find a solution can you help me out?? – satheesh Dec 14 '15 at 11:32
  • Possible duplicate of [\_kCFStreamErrorCodeKey=-2102 only with wifi of some ISPs](https://stackoverflow.com/questions/33751432/kcfstreamerrorcodekey-2102-only-with-wifi-of-some-isps) – Sahil Kapoor Aug 07 '18 at 19:45

2 Answers2

0

Method 1: set timeoutIntervalForRequest in sessionConfig:

let sessionConfig = URLSessionConfiguration.default
sessionConfig.timeoutIntervalForRequest = 180 // use a bigger number if needed
let session = URLSession(configuration: sessionConfig)
let task = session.dataTask(with: request, completionHandler: { data, response, error in
    completionHandler(data, response, error)
})

Method 2: set timeoutInterval for request:

var request = URLRequest(url: url)
request.httpMethod = "GET"
request.timeoutInterval = 180 // use a bigger number if needed
let task = URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in
    completionHandler(data, response, error)
})
fujianjin6471
  • 5,168
  • 1
  • 36
  • 32
-2

Increase the timeout interval like 150 secs .

saraman
  • 568
  • 7
  • 19