How do I properly implement Observable.zip with Bifunction?
One returns a Single, one returns an Observable, not sure if this possible.
Thanks for the comment this is my updated. I'm not sure what to put in the BiFunction below
Observable.zip(
mCurrentUserViewModel.getUserProfile(getUserProfileRequest).toObservable(),
mTopicsByCurrentUserViewModel.getTopicsByCreator(getTopicsByCreatorRequest)),
new BiFunction<Single<GetUserProfileResponseBody>, Observable<Response<GetTopicsByCreatorResponseBody>>, **what do i put here**>()
);
you have two options to do so.
toSingle()
converts an Observable which emits a single item to a Single that emits this item
toObservable
converts a Single into an Observable.
for example : using toObservable()
1-create your observables
private Observable<String> getObservable() {
return Observable.just("observable");
}
private Single<String> singleObservable() {
return Single.just("single");
}
private Observable<String> mergedObservable() {
return Observable.zip(getObservable(), singleObservable().toObservable(), new BiFunction<String, String, String>() { //the result of merging is also String
@Override
public String apply(String s, String s2) {
return s + s2; //or if you are using different objects create a model AandB. where A and B are the result of a seperate observers.
}
});
}
2-merge them
mergedObservable().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String s) {
Log.d(tag,s);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
OUTPUT:observablesingle
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