简体   繁体   中英

From completable to observable RxJava

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 ?

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) }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM