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)
}
}