0

I am trying to use RxAlamofire with retry() but I can't make it work. I tried to add retry() block to different positions but it doesn't make any difference.

If I add retry(3) to end of apiClient.get(type: .posts).retry(3) it seems to work. Is there a way to add retry logic to ApiClient class? Thanks.

class ApiClient {
    var baseURL:String

    init(baseURL:String) {
        self.baseURL = baseURL
    }

    func get(type:ApiType) -> Observable<[Post]>{
        return RxAlamofire
            .request(.get, baseURL + "/someurl") //doesn't exist
            .flatMap {
            $0.validate(statusCode: 200..<300)
            .rx.json()
            }.retry(3)
            .map{(data) -> [Post] in
                var posts = [Post]()
                // parse it
                return posts
        }
    }
}
    let apiClient = ApiClient(baseURL: "https://jsonplaceholder.typicode.com")
    apiClient.get(type: .posts)
        .subscribe(onNext:  { [weak self] posts in
            self?.objects = posts
            self?.tableView.reloadData()

            }, onError: { error in
                print(error)

        }).addDisposableTo(disposablebag)
Meanteacher
  • 2,031
  • 3
  • 17
  • 48
  • Is the print statement in onError executed? – tomahh Dec 16 '16 at 03:09
  • How do you know the observable is not retried? Have you try adding a `do(onError: { _ in print("error occured") })` before the `flatMap` call in `get(type:ApiType)`? – tomahh Dec 16 '16 at 07:52
  • I checked the logs and I only saw error one time. However if I add retry like apiClient.get(type: .posts).retry(3) then I see 3 error messages. – Meanteacher Dec 16 '16 at 09:03
  • Add a .debug operator after retry and please attach your output to your question. My guess is it is working right now, though! – Iman Nia Feb 15 '19 at 16:06

0 Answers0