Canonical way to convert Completable to Single?

别等时光非礼了梦想. 提交于 2020-02-24 11:04:14

问题


I have an RxJava Completable that I want to execute, then chain to a Single<Long>. I can write it like this:

return Completable.complete().toSingleDefault(0L).flatMap { Single.just(1L) }

but this seems unnecessarily complicated. I would have thought Completable#toSingle() would do the job, but if I write:

Completable.complete().toSingle { Single.just(1L) }

I get errors. Is there a missing function in Completable or am I overlooking something?


回答1:


You probably want to use the andThen opeator, which will subscribe to the source you send to it after the Completable completes.

return Completable.complete()
    .andThen(Single.just(1L))

As @akarnokd said, this is a case of non-dependent continuations.

In case of your source needing to be resolved at runtime, this would be a deferred-dependent continuation, and you'd need to defer it:

return Completable.complete()
    .andThen(Single.defer(() -> Single.just(1L)))


来源:https://stackoverflow.com/questions/50662480/canonical-way-to-convert-completable-to-single

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!