Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get 2 values in onNext() of subscriber in rxjava android?

I've an observable like this

Observable.zip(observable, extObs, new Func2<List<UserProfile>, ArrayList<Extension>, UserProfile>() {
        @Override
        public UserProfile call(List<UserProfile> userProfiles, ArrayList<Extension> extensions) {


            return userProfiles.get(0);
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).unsubscribeOn(Schedulers.io()).subscribe(new Subscriber<UserProfile>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
        }

        @Override
        public void onNext(UserProfile userProfile) {
            profileListener.onProfileSet(userProfile);
        }
    });
}

I need to pass the ArrayList in the methodprofileListener.onProfileSet(userProfile); as profileListener.onProfileSet(userProfile,extensions);

Is it possible to do so in zip or is there any other methods of rxjava to solve such type of problems?

like image 828
theanilpaudel Avatar asked Mar 23 '16 11:03

theanilpaudel


People also ask

When the onNext () method of the subscriber is called?

After a Subscriber calls an Observable 's subscribe method, the Observable calls the Subscriber's Observer. onNext(T) method to emit items. A well-behaved Observable will call a Subscriber's Observer. onCompleted() method exactly once or the Subscriber's Observer.

What is onNext in RxJava?

onNext(): This method is called when a new item is emitted from the Observable. onError(): This method is called when an error occurs and the emission of data is not successfully completed. onComplete(): This method is called when the Observable has successfully completed emitting all items.

What is RxJava subscriber?

In RxJava 2 org. reactivestreams. Subscriber is an interface complying to Reactive Streams specification. The main difference from Observable is that new Subscriber supports backpressure. Observer is subscribed to Observable , and Subscriber is subscribed to Flowable (implements org.


1 Answers

You have to do exactly what cricket_007 suggested in the comment.

For example like this:

  1. Create a class that would represent combined results of your observables:

class CombinedResults {
    public UserProfile userProfile;
    public List<Extension> extensions;

    public CombinedResults(UserProfile userProfile, List<Extension> extensions) {
        this.userProfile = userProfile;
        this.extensions = extensions;
    }
}

(Alternatively you could use Pair class)

  1. Use an object of CombinedResults (or Pair) in your Observable.zip Func2.

Observable.zip(observable, extObs,
        new Func2<List<UserProfile>, ArrayList<Extension>, CombinedResults>() {
            @Override
            public CombinedResults call(List<UserProfile> userProfiles, ArrayList<Extension> extensions) {
                return new CombinedResults(userProfiles.get(0), extensions);
            }
        })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .unsubscribeOn(Schedulers.io())
        .subscribe(new Subscriber<CombinedResults>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(CombinedResults combined) {
                profileListener.onProfileSet(combined.userProfile, combined.extensions);
            }
        });
like image 71
Bartek Lipinski Avatar answered Oct 12 '22 07:10

Bartek Lipinski