I have the following RxJava2 Kotlin code:
val tester = Completable.complete()
.andThen(SingleSource<Int> { Single.just(42) })
.test()
tester.assertComplete()
tester.assertValue(42)
This simulates having a Completable observable (imagine a simple update operation to an API) and then a Single observable (image a get operation on an API). I want to concatenate the two observables in a way that, when the Completable finishes, the Single runs and finally I get the onSuccess event on my observer (the Int 42).
However this test code is not working. The assertion fails with the following error:
java.lang.AssertionError: Not completed
(latch = 1, values = 0, errors = 0, completions = 0))
I am not able to understand what I am doing wrong, I expect that the Completable emits onComplete on subscription, and then the Single subscribes, and my observer (tester
) gets an onSuccess event with the value 42, but seems that the subscription stays "suspended" not emitting anything.
The idea is similar to the one found in this blog post: https://android.jlelse.eu/making-your-rxjava-intentions-clearer-with-single-and-completable-f064d98d53a8
apiClient.updateMyData(myUpdatedData) // a Completable
.andThen(performOtherOperation()) // a Single<OtherResult>
.subscribe(otherResult -> {
// handle otherResult
}, throwable -> {
// handle error
});
The problem is Kotlin with its ambiguous use of curly braces:
.andThen(SingleSource<Int> { Single.just(42) })
You create a SingleSource
that does noting to its SingleObserver
, but that is hidden by the Kotlin syntax. What you need is plain use:
.andThen(Single.just(42))
or deferred use
.andThen(Single.defer { Single.just(42) })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With