RxSwift: Mapping a completable to single observable?

Yasir picture Yasir · May 28, 2017 · Viewed 14.9k times · Source

I am trying to do a couple of actions in sequence, using RxSwift and am unsure how to get it working.

The problem is returning a Single observable, where the success/error depends on whether a Completable call succeeds or fails.

My code attempt looks roughly like this:

func doSomething(with value: SomeType) -> Single<SomeType> {
    return repository.replace(with: value) // replace() returns a completable
        .asObservable()
        .flatMap { () -> Single<SomeType> in
            return Single.just(value)
    }
}

Error on line 4 (flatMap):

Cannot convert call result type 'Observable<_.E>' to expected type 'PrimitiveSequence< SingleTrait, SomeType >' (aka 'PrimitiveSequence< SingleTrait, SomeType >')

How can I map this completable to a single?

Answer

Wicket picture Wicket · Nov 24, 2017

I am not sure about RxSwift, but in RxJava you could to the following

repository.replace(with: value).andThen(Single.just(value))