RxSwift - Recursive Observables?

自古美人都是妖i 提交于 2021-01-28 05:30:20

问题


Learning RxSwift - Here's my Problem:

i have a webservice that fetches data using an active access token, whenever the token expired , then first call the token generate api and then call the current request to run again. so that it will have an active access token to valid results.

but i have problem in getting the response for token and then call the prev. request?

so i tried adding an observable request , then in response check if the token is invalid, then call another observable to return an active token, once token is received , call the older request again.

func apirequest(_ urlConvertible:URLRequestConvertible) -> Observable<[String:AnyObject]> {
  return Observable.create({ observer  -> Disposable in
         let _ = Alamofire.request(urlConvertible).responseJSON  
                      { response in 
         if isTokenExpired() {
             self.generateToken().subscribe(onNext: response {
               self.apirequest(oldRequest)
          })
        }
      }
      return Disposables.create()
   })
}

i was expecting like any Rx operators or any ideas to try?

Thanks


回答1:


I wrote an article about how to do this: https://medium.com/@danielt1263/retrying-a-network-request-despite-having-an-invalid-token-b8b89340d29

Wrap your network calling code in something like this:

/// Builds and makes network requests using the token provided by the service. Will request a new token and retry if the result is an unauthorized (401) error.
///
/// - Parameters:
///   - response: A function that sends requests to the network and emits responses. Can be for example `URLSession.shared.rx.response`
///   - tokenAcquisitionService: The object responsible for tracking the auth token. All requests should use the same object.
///   - request: A function that can build the request when given a token.
///   - Returns: response of a guaranteed authorized network request.
public func getData<T>(response: @escaping (URLRequest) -> Observable<(response: HTTPURLResponse, data: Data)>, tokenAcquisitionService: TokenAcquisitionService<T>, request: @escaping (T) throws -> URLRequest) -> Observable<(response: HTTPURLResponse, data: Data)> {
return Observable
        .deferred { tokenAcquisitionService.token.take(1) }
        .map { try request($0) }
        .flatMap { response($0) }
        .map { response in
            guard response.response.statusCode != 401 else { throw TokenAcquisitionError.unauthorized }
            return response
        }
        .retryWhen { $0.renewToken(with: tokenAcquisitionService) }
}

The article and accompanying gist shows how to write the tokenAcquisitionService and includes unit tests.



来源:https://stackoverflow.com/questions/57555764/rxswift-recursive-observables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!