4

Trying to call the web service as follows , and experiencing random "The network connection was lost" error causing app crash on device as well as simulator.

 Alamofire.request(url, method: .post, parameters: ["limit" : "3" , "offset" : self.storeArray?.count ?? 0], encoding: URLEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

                switch(response.result) {
                case .success(_):

my error bellow

Optional(Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x608000a5f950 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x60800029fea0 [0x1136c0df0]>{length = 16, capacity = 16, bytes = 0x10020050a63e1c720000000000000000}, _kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=http://lokomart.com/services/getSearchResultData, NSErrorFailingURLKey=http://lokomart.com/services/getSearchResultData, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.})

// I have using php backend with godaddy deluxe hosting.

and also working with swift 3

vishal dharankar
  • 7,536
  • 7
  • 57
  • 93
user3091160
  • 145
  • 2
  • 13

3 Answers3

3

I faced this issue and spend more than 1 week to fix this. AND i just solved this issue by changing Wifi connection.

Ahtazaz
  • 903
  • 8
  • 21
  • Cool solution :) – Alok Oct 09 '20 at 08:04
  • OMG! That worked! Thanks. Spent many hours debugging swift code thinking it was my fault. – bauhaus9 Feb 26 '21 at 21:03
  • @bauhaus9 sounds great. – Ahtazaz Feb 27 '21 at 20:04
  • Update: I have two wifi networks. The reaso I could NOT connect to my target host was because I had a network rule that was blocking any connectivity to that target host. On the wifi network where it DID work, I had a clean, unblocked connection. Once I fixed the rule on the misbehaving network, both networks allowed access. So, my code was fine, it was the network! – bauhaus9 Feb 28 '21 at 22:27
2

There is no direct solution to this issue , I have tried increasing PHP backend processing timeout to 60 sec also tried changing Apache server timeout to 600 from 300, it only reduces the frequency , not best solution I think is to catch the failure and then either recreate the request and send it or just stop do something else.

Alamofire.request(url, method: .post, parameters: ["limit" : "3" , "offset" : self.storeArray?.count ?? 0], encoding: URLEncoding.default, headers: nil).validate().responseJSON { (response:DataResponse<Any>) in

    switch(response.result) {
        case .success(_):
         break;
        case .failure(_ let error): {
         //redo all the work or stop 
         break;
         } 

or what I am doing is as follows

if(response.result.isFailure) {
     print("Error occured")
     if(self.retryCounter! < 2) {                    
         self.retryCounter! += 1
         //recreating post request and sending again
          return
     }
     else {
         return
     }
}

Above code does the job well. Notice that I edited your code and added "validate" method, without which some of the result data doesn't get updated, like "isFailure" property is always false. I hope this gets you through the issue.

Chandresh Kachariya
  • 667
  • 2
  • 13
  • 31
vishal dharankar
  • 7,536
  • 7
  • 57
  • 93
1

Today, i had the same error. And i remember the property added for apple into iOS 9 NSAppTransportSecurity. You can try disable only for test, adding this:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

You can to do this configuration better following this answer https://stackoverflow.com/a/31623388/2711766