Search code examples
swiftalamofirensurlrequest

Setting timeoutInterval on Alamofire DataRequest


I'm making a custom class that takes an Alamofire DataRequest in it's initializer. Now I want to add a timeoutInterval to it, but I'm getting the compiling error

Value of type 'DataRequest' has no member 'timeoutInterval'

Here is the code:

init(request: DataRequest, timeoutInterval: Double = 10) {
    request.timeoutInterval = timeoutInterval // <- compile error here
    self.request = request
}

Obviously Alamofire DataRequest doesn't have that property. But is there any other way to specify the timeout to a request in this way (without using a SessionManager preferable)? URLRequest has it, so it should be possible somehow, but I cannot figure out how.

I know that this question has been asked before here on Stack Overflow, but I cannot find any answer that fits this situation.


Solution

  • In Alamofire 5.1 we've added a requestModifier parameter to the top level request methods that gives you access to the URLRequest that will be performed.

    In Alamofire 4 you have a few, less elegant options. One straightforward way to set it is to use a RequestAdapter that will set it as part of the request pipeline. Another, more involved option, is to move from the top level request method that takes individual parameters, such as headers, to the API that takes a URLRequestConvertible value. That way you have full control over the URLRequests that are performed by Alamofire on your behalf.