Alamofire: Per Request retriers

Created on 14 Aug 2017  路  7Comments  路  Source: Alamofire/Alamofire

I think that retrier variable in SessionManager is not very good...
What do you think put it into the Request?
use way like this:

Alamofire.request("https://temp.com", method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).response { (response) in



        }.retrier(_ shouldRetry: Bool, _ timeDelay: TimeInterval) //There may be other related parameters
feature request request adapter request retrier

Most helpful comment

I might be a bit late to the party, but I'm using my homegrown Rx interface to SessionManager which allows me to do something like this:

func methodName(data: ServiceName.Method.RequestData/*Encodable*/) -> Single<ServiceName.Method.ResponseData/*Decodable*/> {
    return sessionManager
        .rx
        .post(url, body: data/*, ...*/)
        .object()
}

// ...

    let disposable = service
        .methodName(data: data)
        .timeout(configuration.timeout, scheduler: configuration.timeoutScheduler)
        .retry(configuration.retryCount) // automatically retries failed request
        .retryWhen(retryHandler()) // invokes custom retry handler to do more sophisticated retry logic like exponential backoff or presenting a UI
        .subscribe(/*...*/)

This way, the caller has full control over the retry logic. And readily-available instruments in Rx are quite powerful, so I don't even need that functionality in Alamofire.

All 7 comments

This is something that we're going to investigate in-depth in AF5 @gaoyuexit. Having a single retrier per SessionManager can be a bit limiting when you are trying to funnel multiple authentication systems through a single session manager. More updates to come...

I have the same problem at the moment. But to channel it in the request seems wrong on the other hand. I think a great addition would be to hold multiply retriers and you can cancel the current one. In this way you could add specific logic for specific requests in the retriers.

I might be a bit late to the party, but I'm using my homegrown Rx interface to SessionManager which allows me to do something like this:

func methodName(data: ServiceName.Method.RequestData/*Encodable*/) -> Single<ServiceName.Method.ResponseData/*Decodable*/> {
    return sessionManager
        .rx
        .post(url, body: data/*, ...*/)
        .object()
}

// ...

    let disposable = service
        .methodName(data: data)
        .timeout(configuration.timeout, scheduler: configuration.timeoutScheduler)
        .retry(configuration.retryCount) // automatically retries failed request
        .retryWhen(retryHandler()) // invokes custom retry handler to do more sophisticated retry logic like exponential backoff or presenting a UI
        .subscribe(/*...*/)

This way, the caller has full control over the retry logic. And readily-available instruments in Rx are quite powerful, so I don't even need that functionality in Alamofire.

Yeah not everyone using rx, so we need this directly on alamofire on the request base.

I've solved the offline retrier issue earlier in a project by creating a custom Codable APIRequest struct that holds:

  • parameters dictionary
  • url
  • method
  • encoding
    Every time a request fails, I saved that request on disk locally using FileManager, and I used NetworkReachabilityManager to observe network status changes and retry saved requests, removing the ones that complete successfully.

Maybe a similar approach could be followed for this?
I've been meaning to submit a PR for this, but just saw this issue and thought I'd share this with you 馃槃

This feature has been built out in #2704 and merged into master. It will ship shortly as part of Alamofire 5.0.0-beta.2. 馃馃徎

yay!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yamifr07 picture yamifr07  路  3Comments

hengchengfei picture hengchengfei  路  3Comments

sarbogast picture sarbogast  路  3Comments

matthijsotterloo picture matthijsotterloo  路  3Comments

yokesharun picture yokesharun  路  3Comments