Home > Software design >  From completable to observable RxJava
From completable to observable RxJava

Time:05-20

I have the method which return completable

interface MyClass{
  fun getSomething(): Completable
}

I would like to map it to Observable.just(true) if it is complete or Observable.just(false) if it is not complete/error.

something like this (pseudo code)

  getSomething().doIf {
       if (it.onComplete){
          return Observable.just(true)
    } else {
          return Observable.just(false)
    }
}

Is there something like my made up method doIf ?

I was trying to achieve it using Observable.fromcallable{} or myClass.getSomething().toObservable but nothing works.

Is is possible to achieve that and how to do that ?

CodePudding user response:

Convert it to Single, then to Observable:

getSomething()
.toSingleDefault(true)
.onErrorReturnItem(false)
.toObservable()

or convert to Observable and flatMap the signals

getSomething()
.toObservable()
.flatMap(
    { Observable.never<Boolean>() }, 
    { Observable.just(false) }, 
    { Observable.just(true) }
)
  • Related