j2emanue
j2emanue

Reputation: 62519

RxJava2 - how to observe an already initialised empty observable once it changes?

so i have an Single that has already been initalized as a class member. it gets intialized like this:

var myObservable: Single = Observable.empty().toSingle()

this is defined as a class member.

Afterwards,later on after the class has already been initalized i finally get the actually response and i want this myObservable to now be the response :

  private fun callback(response:Response) {
                myObservable= Single.just(response)//now look, myObservable got changed, but im watching it in the zipWith call. how can i tell zip to watch this one now instead of the empty one i initialized ?


            }

but the problem with this is i am zipping two obervables at the very beginning of the class call (and one of them is myObservable) and i am trying to listen for changes in both and take an action. here is the zipWith function:

  someApi.fetchOrders(orderNumber).zipWith<MyResponse,Pair<OrdersResponse,MyResponse>>(myObservable) { t1, t2 ->Pair(t1,t2)}

but i called this when myObservable was still initalized to empty. later when i got a response i changed it but really i assigned a new VALUE to the observable, i did not retain the same one i was watching in the zip. How can i UPDATE myObservable but keep the SAME reference that zipWith is watching ?

i tried changing myObservable into a subject and then when i zip it it looks like this instead:

someApi.fetchOrders(orderNumber).zipWith>(myObservable.toSingle()) { t1, t2 ->Pair(t1,t2)}

but it never executes the Function lambda.

Upvotes: 0

Views: 258

Answers (1)

Tuby
Tuby

Reputation: 3253

zipWith emits when for both Observables emit, and pairs items with the same index. Your subject probably emitted when fetchOrders didn't, so zipWith didn't do anything. You can use subject and change zipWith to subject.withLatestFrom(someApi.fetchOrders()). Once you push something into the subject, withLatestFrom will emit with last value from fetchOrders().

Upvotes: 1

Related Questions