0

I have an iOS app where I call same API too many times in a looping sense. viz. Call first API -> Get response, parse it -> save to local -> Call same API again ... and so on. So in this flow. Somewhere randomly a request gets times out and I get following logs :

 Task <E49E4BD3-882E-4FFE-A6E1-75E28102CC20>.<7> load failed with error 
 Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." 
UserInfo={_kCFStreamErrorCodeKey=-2102, 
NSUnderlyingError=0x600000f28ed0 {Error Domain=kCFErrorDomainCFNetwork 
Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, 
_kCFStreamErrorDomainKey=4}}, 
_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <E49E4BD3-882E- 
4FFE-A6E1-75E28102CC20>.<7>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <E49E4BD3-882E-4FFE-A6E1-75E28102CC20>.<7>"
), NSLocalizedDescription=The request timed out., 
NSErrorFailingURLStringKey=https://api.domainname.com/getDataApi, 
NSErrorFailingURLKey=https://api.domainname.com/getDataApi, 
_kCFStreamErrorDomainKey=4} [-1001]

It works properly many times . The headers and parameters are correct. I have tried putting those request in NSOperationQueue , Dispatch group. But it works same. I have also tried with NSURLSession instead of Alamofire. No change. It happens frequently on iPhone X and rare on other devices. I am calling API too many times because the data is about 30-40 thousand records and each API returns 1000 records. Sometimes the first API fails sometimes after few hits. Its random. The internet connection is good at time of failure. Streaming, browsing works fine at same time. But the API gives request time out. Tried increasing timeout interval for request. There's no issue with parameters/headers I send because same api works fine some times and fails sometimes. Data returned is also same for all devices.

Shirish
  • 295
  • 3
  • 19
  • Is it your own server or a third-party API? Some APIs have limits of how many calls you can perform, although ideally they shouldn't timeout when you reach it. – EmilioPelaez Mar 12 '19 at 14:20
  • Own server. No limits. – Shirish Mar 12 '19 at 14:21
  • Does your API have any kind of requests/minute limit ? – TibiaZ Mar 12 '19 at 14:21
  • No limits, same api and same flow of looping api works in Android App. – Shirish Mar 12 '19 at 14:28
  • 1
    Rob provides a good solution [here](https://stackoverflow.com/questions/27021896/nsurlsession-concurrent-requests-with-alamofire). – Don Mar 12 '19 at 14:35
  • Request time out, happens! You have to handle it. I suggest to take a look at `RxAlamofire.request` and `.timout` and ofcourse `.retry` in RXSwift programming. – Iman Nia Mar 12 '19 at 14:58

0 Answers0