RxJava 操作符 just和from

来源:互联网 发布:qq堂有mac 编辑:程序博客网 时间:2024/05/16 17:40
    /**     * Returns an Observable that emits a single item and then completes.     * <p>     * <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/just.png" alt="">     * <p>     * To convert any object into an Observable that emits that object, pass that object into the {@code just}     * method.     * <p>     * This is similar to the {@link #from(java.lang.Object[])} method, except that {@code from} will convert     * an {@link Iterable} object into an Observable that emits each of the items in the Iterable, one at a     * time, while the {@code just} method converts an Iterable into an Observable that emits the entire     * Iterable as a single item.     * <dl>     *  <dt><b>Scheduler:</b></dt>     *  <dd>{@code just} does not operate by default on a particular {@link Scheduler}.</dd>     * </dl>     *      * @param value     *            the item to emit     * @param <T>     *            the type of that item     * @return an Observable that emits {@code value} as a single item and then completes     * @see <a href="https://github.com/ReactiveX/RxJava/wiki/Creating-Observables#just">RxJava wiki: just</a>     */    public final static <T> Observable<T> just(final T value) {        return ScalarSynchronousObservable.create(value);    }


/**     * Converts an {@link Iterable} sequence into an Observable that emits the items in the sequence.     * <p>     * <img width="640" height="315" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/from.png" alt="">     * <dl>     *  <dt><b>Scheduler:</b></dt>     *  <dd>{@code from} does not operate by default on a particular {@link Scheduler}.</dd>     * </dl>     *      * @param iterable     *            the source {@link Iterable} sequence     * @param <T>     *            the type of items in the {@link Iterable} sequence and the type of items to be emitted by the     *            resulting Observable     * @return an Observable that emits each item in the source {@link Iterable} sequence     * @see <a href="https://github.com/ReactiveX/RxJava/wiki/Creating-Observables#from">RxJava wiki: from</a>     */    public final static <T> Observable<T> from(Iterable<? extends T> iterable) {        return create(new OnSubscribeFromIterable<T>(iterable));    }



通过方法体可以看出来,入参单个对象返回一个相应的obervable,与from不同的是,subscriber里onNext的参数可以看出两者的区别,一个是返回的list一个是返回的list的item,测试方法如下,测试时可注释掉一个操作符测试另外一个:

public void test() {    List<String> list = new ArrayList<>();    list.add("a");    list.add("b");    list.add("c");    Observable.just(list).subscribe(new Subscriber<List<String>>() {      @Override public void onCompleted() {      }      @Override public void onError(Throwable e) {      }      @Override public void onNext(List<String> strings) {      }    });    Observable.from(list).subscribe(new Subscriber<String>() {      @Override public void onCompleted() {        Log.e("--------onCompleted ", "onCompleted");      }      @Override public void onError(Throwable e) {      }      @Override public void onNext(String s) {        Log.e("--------onNext ", "s : " + s);      }    });  }

from操作符的日志如下:

04-22 17:45:22.092  12883-12883/com.fernandocejas.android10.sample.presentation E/--------onNext﹕ s : a04-22 17:45:22.092  12883-12883/com.fernandocejas.android10.sample.presentation E/--------onNext﹕ s : b04-22 17:45:22.092  12883-12883/com.fernandocejas.android10.sample.presentation E/--------onNext﹕ s : c04-22 17:45:22.092  12883-12883/com.fernandocejas.android10.sample.presentation E/--------onCompleted﹕ onCompleted

因此,from会依次返回list的每个item,而just会直接把list返回相当于输入什么返回什么。


0 0