2

Several people get this error for different reasons. None of the answers I've found have solved my problem.

I use Timberjack to log my Alamofire requests.

All my GET requests work fine and I receive data in JSON. My POSTs on the other hand works around 1 out 10 times every time if the POST includes a JSON body.

The server does not specify any Keep-Alive header.

Deployment target is iOS 9.0

This is my shared Manager with Timberjack:

class HTTPManager: Alamofire.Manager{
    static let sharedManager: HTTPManager = {
        let configuration = Timberjack.defaultSessionConfiguration()
        let manager = HTTPManager(configuration: configuration)
        return manager
    }()
}

Defining the request:

let parameters: [String: AnyObject] = ["status":status]
request = HTTPManager.sharedManager.request(.POST, "\(baseURL)\(uri)", parameters: parameters, encoding: .JSON).validate()

Sending the request:

request!.responseJSON(queue: queue, options: .AllowFragments, completionHandler: { (response) in

                        //Handling the response

})

Most of the time the server receives an empty JSON body. But sometimes it does work and the body is received and the server responds with an OK. When it doesn't work I receive the error:

Error: The network connection was lost.
FAILURE: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." 
UserInfo={NSUnderlyingError=0x12fa47cb0 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "The network connection was lost." 

If someone can explain what's happening here I would be forever grateful :)

EDIT 1 We let the server respond every call with "Connection": "close" which did nothing to help the problem. The app always sends "Connection": "keep-alive" by default and it cannot be changed. Could this be the problem? That the app thinks the connection is open even though it's closed by the server? But even though you wait a few minutes it seems as though the POST call only works at random.

EDIT 2 Even when I wait 30 seconds between GET(s) and/or POST(s). GET always works. POST works at random (rarely). I get the -1005 error on most POSTs. Even though I get the network connection was lost error the server still receive my request but the JSON body is missing.

EDIT 3 - Ugly solution In my response I check for the error code -1005, when I receive this error I just recreate the request and try again. This results in sending around 2-4 POST requests to the server where one POST works and the rest have empty JSON bodies.

hardyfelix
  • 212
  • 3
  • 11

1 Answers1

-2

Restart simulator or kill your app by throwing out from tasks.

Or check more solutions to this error code:

NSURLErrorDomain Code -1005 The network connection was lost

Community
  • 1
  • 1
pedrouan
  • 12,762
  • 3
  • 58
  • 74
  • Thanks but that didn't work. I'm figuring out if it has something to do with the default behaviour of the Keep-Alive header. That the app thinks the sessions is still running even though the server closed it. But still weird that it only happens on POSTs in that case.. – hardyfelix Aug 26 '16 at 16:35