RxSwift: Mapping a completable to single observable?
I am not sure about RxSwift, but in RxJava you could to the following
repository.replace(with: value).andThen(Single.just(value))
Just a few months after I answered this question, a new operator was added to the Completable type. .andThen
. Using it makes this code much simpler:
func doSomething(with value: SomeType) -> Single<SomeType> {
return repository.replace(with: value)
.andThen(Single.just(value))
}
Origional Answer Follows:
Hmm... A Completable never emits an element so there is nothing to flatMap to. I.E. it doesn't make sense to even use the flatMap
operator on a Completable. The only thing you can really do with it is subscribe to it.
Therefore, you need to implement your method like this:
func doSomething(with value: SomeType) -> Single<SomeType> {
return Single<SomeType>.create { observer in
return repository.replace(with: value)
.subscribe(onCompleted: {
observer(.success(value))
}, onError: {
observer(.error($0))
})
}
}
I've tried to work with Observables that don't emit values in the past, before these newfangled types, and I've always found them to be a pain. If I were you, I would convert your replace
method to return a Single<Void>
instead of a Completable
. i.e.:
func replace(with value: SomeType) -> Single<Void>
If you do that, then you can simply:
func doSomething(with value: SomeType) -> Single<SomeType> {
return repository.replace(with: value).map { value }
}
Of course if you can do that, then you might as well have replace(with:)
itself return a Single<SomeType>
.