Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxSwift repeated action

Tags:

swift

rx-swift

I'm switching from RAC and want to have a repeated network request, returning different result types depending on the API of the request.

I want to use an interval, but I don't know how to match the return types.

var loop: Observable<Element> {
    return Observable<Int>.interval(5.0, scheduler: MainScheduler.instance).map { _ in
        // Do network request and return Observable<Element>
    }
}

I need to invoke Observerable.interval with type Int - but return Observable. How would I do that?

like image 985
beseder Avatar asked Feb 10 '16 09:02

beseder


1 Answers

Use flatMap:

var loop: Observable<Element> {
    return Observable<Int>.interval(5.0, scheduler: MainScheduler.instance).flatMap { _ in
        return networkRequest() // returns Observable<Element>
    }
}
like image 89
Evgeny Sureev Avatar answered Dec 19 '22 21:12

Evgeny Sureev