Search code examples
iosswiftalamofire

Alamofire: 'The request timed out' within 1 second regardless of timoutIntervalForRequest


I'm trying to engage Alamofire in my app and faced the following problem in the first request (which I managed to make successfully with URLSession).

When performing a request, within 1-2 seconds I see -1001 The request timed out in the log, although I set a relatively long interval for the session.

This is HttpSession atm:

class HttpSession {

    static let instance = HttpSession()
    let sessionManager: SessionManager?

    private init() {
        let conf = URLSessionConfiguration.default
        conf.timeoutIntervalForRequest = 30
        sessionManager = Alamofire.SessionManager(configuration: conf)
    }
}

Here I make the request:

HttpSession.instance.sessionManager!.request(url, method: .get, parameters: [:], encoding: JSONEncoding.default, headers: headers).responseJSON { response in
    switch response.result {
    case .success:
        print("YAA!")
        break
    case .failure(let error):
        print(error)
        break
    }
}

One odd thing to mention: When I tried to print time before and after to see the time difference, the values were indeed 30 seconds different, though in real life it took 1-2 seconds. I tested it only on simulator. To print seconds I used the following snippet twice, before the request and in the response:

let date: Date = Date()
print(Calendar.current.component(.second, from: date))

Any help will be appreciated, thanks.


Solution

  • First of all, I was wrong as the request indeed timed out each time once the interval has passed. Most of my tests were done with 8 seconds interval and I didn't notice that the timeout log appeared much later.

    Anyway, the problem was with 'parameters' parameter; when putting an empty dictionary parameters: [:] it always timed out, and when replaced with nil it began to work as expected. I find it strange since in the first comment in this issue it is stated that both are supposed to work the same way, and anyhow this type of error is not related to this issue at all.