1
votes

I want to handle each request, and if the response of the request not match the condition , start a new request and get the response. How can I restart the old request

here is my code now

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
Try using .switchLatest() as described here - stackoverflow.com/a/47040856/856588Maxim Volgin

1 Answers

0
votes

You should subclass MoyaProvider, override request method and there you can check response from the api, and retry if needed.