Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Observable zip with Bifunction Single and Observable?

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**>()
    );
like image 897
bycfly Avatar asked Nov 08 '22 11:11

bycfly


1 Answers

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

like image 100
Bishoy Kamel Avatar answered Dec 16 '22 05:12

Bishoy Kamel