如何使用moya,rxswift重新启动网络请求

时间:2017-10-28 11:07:03

标签: ios swift rx-swift moya

我想处理每个请求,如果请求的响应与条件不匹配,则启动新请求并获取响应。如何重新启动旧请求

这是我现在的代码

static func request(target: API) -> Observable<Response> {
        let actualRequest = provider.request(target)
        return self.provider.request(target).flatMapLatest { (response) -> Observable<Response> in
            let responseModel = ResponseModel(data:response.data)

            if responseModel.code == -405 {
                let refreshToken = User.shared?.refreshToken
                self.provider.request(.refreshToken(refresh: refreshToken!)).flatMap({ response -> Observable<String> in
                   return Observable.just("")

                }).shareReplay(1).subscribe(onNext: { refreshToken in
                  // here I get a new token, how to retry the actualRequest , or how to start a new network request with the target 
                }, onError: { (error) in

                },onCompleted: { _ in

                })

            }

                return Observable.just(response)
        }
    }

1 个答案:

答案 0 :(得分:0)

您应该继承MoyaProvider的子类,覆盖请求方法,然后您可以检查来自api的响应,并在需要时重试。