From ea628496f1451b607bc899dcb98adc5cc48c7266 Mon Sep 17 00:00:00 2001 From: Vodovozov Gleb Date: Fri, 23 Oct 2015 12:24:07 +0800 Subject: [PATCH] when receiving error from GitHubSearchRepositoriesAPI wait while network is reachable again and retry query --- ...tHubSearchRepositoriesViewController.swift | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/RxExample/RxExample/Examples/AutoLoading/GitHubSearchRepositoriesViewController.swift b/RxExample/RxExample/Examples/AutoLoading/GitHubSearchRepositoriesViewController.swift index 4bef1f77..c68c4e98 100644 --- a/RxExample/RxExample/Examples/AutoLoading/GitHubSearchRepositoriesViewController.swift +++ b/RxExample/RxExample/Examples/AutoLoading/GitHubSearchRepositoriesViewController.swift @@ -35,6 +35,15 @@ class GitHubSearchRepositoriesViewController: ViewController, UITableViewDelegat let tableView = self.tableView let searchBar = self.searchBar + // init reachability to check internet connection + let reachability:Reachability? + do{ + reachability = try Reachability.reachabilityForInternetConnection() + }catch let error{ + print("cannot create reachability - \(error)") + reachability = nil + } + let allRepositories = repositories .map { repositories in return [SectionModel(model: "Repositories", items: repositories)] @@ -52,6 +61,7 @@ class GitHubSearchRepositoriesViewController: ViewController, UITableViewDelegat return section.items.count > 0 ? "Repositories (\(section.items.count))" : "No repositories found" } + // reactive data source allRepositories .bindTo(tableView.rx_itemsWithDataSource(dataSource)) @@ -72,7 +82,15 @@ class GitHubSearchRepositoriesViewController: ViewController, UITableViewDelegat return just(.Repositories([])) } else { return GitHubSearchRepositoriesAPI.sharedAPI.search(query, loadNextPageTrigger: loadNextPageTrigger) - .catchErrorJustReturn(.Repositories([])) + .retry(3) + .catchError{ (e) -> Observable in + reachability? + .rx_reachable + .skipWhile { $0 != .Reachable } + .flatMap { _ in failWith(e)} + ?? failWith(e) + } + .retry() } } .switchLatest()