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
- RxJava 操作符 just和from
- rxjava 操作符 just,from,scan,map
- rxJava just和from区别
- RxJava 创建操作符 just
- RxJava 1.x from()和just()用法、区别和举例
- RxJava之from操作符
- Android函数响应式编程——必学的RxJava创建操作符create、just、from、interval、range、repeat
- RxJava之just与from源码解读
- RX系列三 | RxJava | create | from | interval | just | range | filter
- 初涉RxAndroid .from() .just() 以及线程操作
- just操作符
- [RxJava笔记]RxJava操作符
- RxJava 过滤操作符 distinct和elementAt
- RxJava操作符 debounce 和 distinctUntilChanged
- RxJava操作符repeatWhen()和retryWhen()
- RxJava之条件和布尔操作符
- RxJava操作符repeatWhen()和retryWhen()
- RX操作符之Observable的创建方式一(just、from、repeat、repeatWhen)
- MAC安装oh my zsh
- cocos2dx 4.3 ToggleVisibility
- HDU 1800 Flying to the Mars
- 用Quick-Cocos2d-x 3.3简单开发微信打飞机 -03 添加爆炸动画和子弹与敌机的碰撞
- 基于socket的ftp实现
- RxJava 操作符 just和from
- strpbrk和strcspn这两个函数的区别
- 创建链接
- Log4J 配置
- IOS控件 动画操作 与 CALayer 简单操作
- 三++14
- 第17题
- iOS Block在ARC/非ARC下的使用总结
- ATS 4.2.3隐藏服务器名称及版本号的方法