Matthew
Matthew

Reputation: 366

Swift Alamofire RequestInterceptor not called if using SessionDelegate

I am trying to refresh my authentication credential for my request using Alamofire's RequestInterceptor.

I noticed I am unable to enter the validate() portion of the interceptor as my breakpoints show me I am entering my SessionDelegate handlers instead. More precisely the urlSession(_:dataTask:didReceive:completionHandler:) and urlSession(_:task:didCompleteWithError:) methods of my SessionDelegate class seems to swallow the logic for the RequestInterceptor. If I remove these two handlers from my SessionDelegate I am able to enter the validate() code which then triggers the refresh method on my Authenticator class.

Is there a way to force the RequestInteceptor to intercept my request and refresh my credential if needed before it enters my SessionDelegate handlers? Removing the handlers from my SessionDelegate is not an option as I need to perform specific business logic in there.

Code:

        self.session = Session(delegate: mySessionDelegate, interceptor: myRequestInterceptor)
        self.session.request(myURL)
            .validate({ _, response, data -> DataRequest.ValidationResult in
                let statusCode = response.statusCode
                //this code is not entered if I have my SessionDelegate handler
                if 200 ... 299 ~= statusCode { return .success(Void()) }
                return .failure(AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: statusCode)))
            })
            .resume()

Upvotes: 0

Views: 694

Answers (1)

Jon Shier
Jon Shier

Reputation: 12770

If you're writing a custom SessionDelegate subclass (which isn't recommended; there's almost always a better way to accomplish what you need), it's your responsibility to call super in your overridden methods, as they contain important events. Otherwise you'll break important Alamofire events, as you've found.

Upvotes: 1

Related Questions