问题
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