Skip to content

Instantly share code, notes, and snippets.

Embed
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(
RxTimeInterval.seconds(0),
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)) })
.asObservable()
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
You can’t perform that action at this time.