Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava - Just vs From

Tags:

java

rx-java

I'm getting the same output when using Observable.just vs Observable.from in the following case:

 public void myfunc() {
 //swap out just for from here and i get the same results,why ?
        Observable.just(1,2,3).subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {
                Log.d("","all done. oncompleted called");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Integer integer) {
                Log.d("","here is my integer:"+integer.intValue());
            }
        });

    }

I thought just was just supposed to emit a single item and from was to emit items in some sort of list. Whats the difference ? I also noted that just and from take only a limited amount of arguments. So Observable.just(1,2,3,4,5,6,7,8,-1,-2) is ok but Observable.just(1,2,3,4,5,6,7,8,-1,-2,-3) fails. Same goes for from, I have to wrap it in a list or array of sorts. I'm just curious why they can't define unlimited arguments.

UPDATE: I experimented and saw that just does not take a array structure it just takes arguments. from takes a collection. so the following works for from but not for just:

 public Observable myfunc() {
    Integer[] myints = {1,2,3,4,5,6,7,8,-1,-2,9,10,11,12,13,14,15};
   return  Observable.just(myints).flatMap(new Func1<Integer, Observable<Boolean>>() {
        @Override
        public Observable<Boolean> call(final Integer integer) {
            return Observable.create(new Observable.OnSubscribe<Boolean>() {
                @Override
                public void call(Subscriber<? super Boolean> subscriber) {
                    if(integer.intValue()>2){
                        subscriber.onNext(integer.intValue()>2);

                    }
                }
            });
        }
    });

}

I am assuming this to be the clear difference then, correct ?

like image 270
j2emanue Avatar asked Jun 13 '15 13:06

j2emanue


People also ask

What is just in RxJava?

RxJava 1․ RxJava implements this operator as just . It accepts between one and nine items as parameters, and returns an Observable that emits these items in the same order as they are given in the parameter list. just does not by default operate on any particular Scheduler.

What is single just?

Single. just(x) evaluates the x immediately in the current thread and then you're left with whatever was the result of x , for all subscribers.

What is just in RxSwift?

RxSwift: Just Operator just operator creates an observable sequence containing just a single element and a . completed event. Type inference means we don't need to define the type.

Why is RxJava so popular nowadays?

It means, RxJava can be able to do some tasks at the same time immediately such as calling multiple web services in the background, error-handling, and view-handling without decreasing the responsiveness of an Android app.


3 Answers

Difference between just() and from():

All though just() and from() appears to be doing the same work, it differs in number of emissions.

just() – Makes only 1 emission. Observable.just(new Integer[]{1, 2, 3}) makes one emission with Observer callback as onNext(Integer[] integers)

fromArray() – Makes N emissions. Observable.fromArray(new Integer[]{1, 2, 3}) makes three emission with Observer callback as onNext(Integer integer)

like image 35
Zubair Rehman Avatar answered Sep 20 '22 02:09

Zubair Rehman


The difference should be clearer when you look at the behaviour of each when you pass it an Iterable (for example a List):

Observable.just(someList) will give you 1 emission - a List.

Observable.from(someList) will give you N emissions - each item in the list.

The ability to pass multiple values to just is a convenience feature; the following are functionally the same:

Observable.just(1, 2, 3); Observable.from(1, 2, 3); 
like image 94
Adam S Avatar answered Sep 24 '22 02:09

Adam S


from works mostly with data structures (arrays and iterable) and futures, so the obtained Observable will emit single items from those data structures or futures.

just treats everything as item regardless it is an array item or integer item. The confusion around just is generated by the fact that there are a few just variants that can accept up to 10 arguments.

So in fact, you might interpret all those just variants like they emit, respectively, "just" one item, or "just" two items, "just" three items and so on...

like image 41
vortex.alex Avatar answered Sep 24 '22 02:09

vortex.alex