问题描述
在以下情况下使用 Observable.just
与 Observable.from
时,我得到相同的输出:
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());
}
});
}
我认为 just
应该只发出一个项目,而 from
应该发出某种列表中的项目.有什么不同 ?我还注意到 just
和 from
只接受有限数量的参数.所以 Observable.just(1,2,3,4,5,6,7,8,-1,-2)
没问题,但是 Observable.just(1,2,3,4,5,6,7,8,-1,-2,-3)
失败.from
也是如此,我必须将它包装在一个列表或排序数组中.我只是好奇为什么他们不能定义无限的参数.
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.
更新:我进行了试验,发现 just
不采用数组结构,它只采用参数.from
需要一个集合.所以以下适用于 from
但不适用于 just
:
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 ?
推荐答案
当您向每个传递一个 Iterable
(例如一个 列表
):
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)
会给你 1 次发射 - 一个 List
.
Observable.just(someList)
will give you 1 emission - a List
.
Observable.from(someList)
会给你 N 个排放量——列表中的每一项.
Observable.from(someList)
will give you N emissions - each item in the list.
将多个值传递给just
的能力是一个方便的特性;以下功能相同:
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);
这篇关于RxJava - Just vs From的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!