when receiving error from GitHubSearchRepositoriesAPI wait while network is reachable again and retry query

This commit is contained in:
Vodovozov Gleb 2015-10-23 12:24:07 +08:00
parent e8cb6721b7
commit ea628496f1
1 changed files with 19 additions and 1 deletions

View File

@ -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<SearchRepositoryResponse> in
reachability?
.rx_reachable
.skipWhile { $0 != .Reachable }
.flatMap { _ in failWith(e)}
?? failWith(e)
}
.retry()
}
}
.switchLatest()