tilo tilo - 8 months ago 59
Swift Question

Updating periodically with RxSwift

I use the following setup to retrieve objects (e.g. GitHub issues) from an API. This works fine.

let provider: RxMoyaProvider<GitHub>
let issues: Driver<[IssueViewModel]>

init(provider: RxMoyaProvider<GitHub>) {
self.provider = provider
vehicles = provider.request(.Issue)
.mapArray(Issue.self, keyPath: "issues")
.asDriver(onErrorJustReturn: [])
.map { (models: [Issue]) -> [IssueViewModel] in
let items = models.map {
IssueViewModel(name: $0.name,
description: $0.description
return items

Now I'd like to periodically update the list of issues (e.g., every 20 seconds). I thought about an
to accomplish this task, but I guess there might be a clean(er) solution (i.e. in a Rx manner) that I didn't think about.

Any hint in the right direction is highly appreciated.


This is very similar to this question/answer.

You should use timer and then flatMapLatest:

Observable<Int>.timer(0, period: 20, scheduler: MainScheduler.instance)
    .flatMapLatest { _ in
    .mapArray(Issue.self, keyPath: "issues")
    // ...