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)