I want to handle timeout error with Alamofire as follow.
User initiate request with button press
If timeout occurs then it will display message with custom view with 2 buttons (cancel & retry)
if user presses retry_button the request should retry
I already made a general class for Alamofire configuration & also add Interceptor Protocol
But I think request will retry automatically without user interaction.
My Code is as follow:
let config = Session.default.session.configuration
config.timeoutIntervalForRequest = 1
public func retry(_ request: Request, for session: Session, dueTo error: Error, completion: @escaping (RetryResult) -> Void) {
//TODO: How to retry request when retry_button press in custom view
}
Thank you for your valuable time to at least read my question.
Solution 1
Make the request with a different configuration which does not have a retrier (not having a retrier means that the
retrymethod will not get called). Then retry the request whenever the user clicks on the Retry button.Solution 2
When making this network request, remove
.validate()from theAlamofirerequest. This way,Alamofirewill not validate whether the response had an unacceptable status code. Theretrymethod will not get called, and we can write the logic to retry the request whenever the user presses the Retry button.Solution 3
(I haven't tested this method at the time of writing this answer.)
If there is really no way to go with Solution 1 or Solution 2, you could try recursively calling the
retrymethod, while waiting for the user to click on the Cancel or Retry button. When the user clicks on the Cancel or Retry button, set a property in a SingletonClass, and use the value of that same property in the retry method to determine whether that request should be retried or cancelled.