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 ?
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.
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.
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.
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.
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)
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);
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...
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