Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Building a Client App From Scratch - NewsManager
import Foundation
import RxSwift
import RxCocoa
class NewsManager {
typealias NewsLoadState = LoadState<ArticlesResponse>
enum LoadError: Error {
case unknownError
private let client = NewsClient()
private let timer = Observable<Int>.timer(
period: RxTimeInterval.seconds(120),
scheduler: ConcurrentDispatchQueueScheduler(qos: .background))
func news() -> Driver<NewsLoadState> {
return timer.flatMapLatest { [client] (count) -> Observable<NewsLoadState> in
let pending: NewsLoadState = (count == 0) ? .loading : .updating
let loaded: Observable<NewsLoadState> = client.headlines()
.map({ .loaded(response: $0) })
.catchError({ Single.just(.failed(error: $0)) })
return Observable.merge([Observable.just(pending), loaded])
}.asDriver(onErrorJustReturn: .failed(error: LoadError.unknownError))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment