'Domain=NSURLErrorDomain Code=-1005 "The network connection was lost in swift5

I am using swift5 & Xcode Version 13.0 (13A233). I am using cocapod

 pod 'Alamofire' , '~> 4.9.1'

I just connect using IP, it is working fine.But when I connect to domain like https://www.example.net it shows

     Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=-4, NSUnderlyingError=0x600002af2b50 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x6000007c8230 [0x1056ddaf0]>{length = 16, capacity = 16, bytes = 0x100201bb772858170000000000000000}, _kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <A49E8D5B-1DE3-487C-B4F7-50376AF188AE>.<3>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
        "LocalDataTask <A49E8D5B-1DE3-487C-B4F7-50376AF188AE>.<3>"
    ), NSLocalizedDescription=The network connection was lost., NSErrorFailingURLStringKey=https://www.example.net/api/second-api, NSErrorFailingURLKey=https://www.example.net/api/second-api, _kCFStreamErrorDomainKey=4}

I have also add App Transport Security Settings like bellow & here is the image enter image description here

Here is the code I have used..

  1. API One:

        func firstApi() {
    
            Alamofire.request("https://www.example.net/api/first-api", method: .post, parameters: [
                "param1":"",
                "param2":"",
            ],headers: headers).responseJSON{ [self](responseData) -> Void in
                secondApi()
            }
    
        }
    
  2. API Two:

          func secondApi() {
    
            Alamofire.request("https://www.example.net/api/second-api", method: .post, parameters: [
                "param1":"",
                "param2":"",
                "param3":"",
            ],headers: headers).responseJSON{ [self](responseData) -> Void in
    
            }
    
        }
    

In the above code, I have called Second API from first API. First API is responding nicely. But in second API which I call from first API response shows above error..

But Only single API called but when I call another API from single API shows error

I don't know what is reason. Please help me to solve the problem..



Solution 1:[1]

I have solve the problem just add

do {
     sleep(1)
   }

My secondApi look like bellow:

  func secondApi() {

   do {
      sleep(1)
     }

   Alamofire.request("https://www.example.net/api/second-api", method: .post, parameters: [
        "param1":"",
        "param2":"",
        "param3":"",
    ],headers: headers).responseJSON{ [self](responseData) -> Void in

    }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Enamul Haque