private static void testCreate() {
Observable.create(new ObservableOnSubscribe
1.1 :create() *** 作符总结
1)创建一个Observable,调用Observable.create();ObservableEmitter是事件的发送器,可以发送多个onNext()方法;一旦发送onComplete(),onError()事件之后,后续的事件将不会再发送;
2)创建一个Observer;里面有四个方法:onSubscribe(),onNext(),onError(),onComplete();
3)创建订阅关系 :Observable.subscribe(observer);
4) Observable.create() 创建 ObservableCreate被观察者 *** 作符
1)快速创建1个被观察者对象(Observable
)
2)发送事件的特点:直接发送 传入的事件
3)Observable.just() 创建 ObservableJust被观察者 *** 作符
2.2:使用示例private static void testJust() {
Observable.just(1,2,3,4,5,6,7,8,9)
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
System.out.println("use Subscribe connect Observable and
Observer");
}
@Override
public void onNext(Integer integer) {
System.out.println("Next event" + integer + " response");
}
@Override
public void onError(Throwable e) {
System.out.println("Error event response");
}
@Override
public void onComplete() {
System.out.println("Complete event response");
}
});
}
打印结果:
use Subscribe connect Observable and Observer
Next event1 response
Next event2 response
Next event3 response
Next event4 response
Next event5 response
Next event6 response
Next event7 response
Next event8 response
Next event9 response
Complete event response
2.3 :just() *** 作符拓展
just(T item) ------这个代码可以引出思考,T 可以是某个业务逻辑返回结果,那么就可以有下面的场景。
1)类似Android网络访问,rxjava能不能直接把helloMicky()放在非主线程访问
2)而把subscribe放在主线程访问呢?这样就解决了android繁琐的callback问题了
3:fromArray() *** 作符 3.1: fromArray() *** 作符特点1)快速创建1个被观察者对象(Observable
)
2)发送事件的特点:直接发送 传入的数组数据
3)实现数组的遍历
4)Observable.fromArray() 创建 ObservableFromArray *** 作符对象
3.2 :代码解析private static void tesrArray() {
Integer[] items = {0,1,2,3,4,5};
Observable.fromArray(items)
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
System.out.println("use Subscribe connect Observable and Observer");
}
@Override
public void onNext(Integer integer) {
System.out.println("Next event" + integer + " response");
}
@Override
public void onError(Throwable e) {
System.out.println("Error event response");
}
@Override
public void onComplete() {
System.out.println("Complete event response");
}
});
}
打印结果:
use Subscribe connect Observable and Observer
Next event0 response
Next event1 response
Next event2 response
Next event3 response
Next event4 response
Next event5 response
Complete event response
4:fromIterable() *** 作符
4.1: fromIterable() *** 作符特点
1)快速创建1个被观察者对象(Observable
)
2) 发送事件的特点:直接发送 传入的集合List
数据
3)集合元素遍历
4)Observable.fromIterable()创建
4.2 :代码示例 private static void testIterable() {
List items = new ArrayList<>();
for (int i = 0; i < 6; i++) {
items.add(i);
}
Observable.fromIterable(items)
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
System.out.println("use Subscribe connect Observable and Observer");
}
@Override
public void onNext(Integer integer) {
System.out.println("Next event" + integer + " response");
}
@Override
public void onError(Throwable e) {
System.out.println("Error event response");
}
@Override
public void onComplete() {
System.out.println("Complete event response");
}
});
}
打印结果
use Subscribe connect Observable and Observer
Next event0 response
Next event1 response
Next event2 response
Next event3 response
Next event4 response
Next event5 response
Complete event response
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)