0

I want to handle timeout error with Alamofire as follow.

  1. User initiate request with button press

  2. If timeout occurs then it will display message with custom view with 2 buttons (cancel & retry)

  3. 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.

nirav
  • 573
  • 5
  • 20

1 Answers1

0

Solution 1

Make the request with a different configuration which does not have a retrier (not having a retrier means that the retry method will not get called). Then retry the request whenever the user clicks on the Retry button.

var customSessionManager = SessionManager()
customSessionManager.retrier = nil
customSessionManager.request( ... ) {
    // request is being made with no retrier. Hence retry method will not be called.
}

Solution 2

When making this network request, remove .validate() from the Alamofire request. This way, Alamofire will not validate whether the response had an unacceptable status code. The retry method 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 retry method, 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.

SingletonClass.retryRequest = false // initial default value
SingletonClass.cancelRequest = false // initial default value
@IBAction func cancelButtonWasPressed(_ sender: AnyObject) {
    SingletonClass.cancelRequest = true
}

@IBAction func retryButtonWasPressed(_ sender: AnyObject) {
    SingletonClass.retryRequest = true
}
public func retry(_ request: Request, for session: Session, dueTo error: Error, completion: @escaping (RetryResult) -> Void) {
    if SingletonClass.retryRequest && !SingletonClass.cancelRequest {
        // This means the user clicked on the Retry button.
        SingletonClass.retryRequest = false // reset the value back to false
        completion(.retry)
    } else if !SingletonClass.retryRequest && SingletonClass.cancelRequest {
        // This means the user clicked on the Cancel button.
        SingletonClass.cancelRequest = false
        completion(.doNotRetry)
    } else {
        // Recursively call the retry method till the user clicks on either the Cancel or Retry button.
        retry(request, for session, dueTo error, completion)
    }
}
Dhruv Saraswat
  • 858
  • 9
  • 13