基本使用方法
Observable<Integer> observable = Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
emitter.onNext(1);
emitter.onNext(2);
emitter.onNext(3);
emitter.onComplete();
}
});
Observer<Integer> observer = new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Integer value) {
Log.d("meee",getClass()+":\n"+"value:"+value);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
};
observable.subscribe(observer);
链式调用
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
emitter.onNext(1);
emitter.onNext(2);
emitter.onNext(3);
emitter.onComplete();
}
}).subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Integer value) {
Log.d("meee", getClass() + ":\n" + "value:" + value);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
补充信息
建立关联之后, 被观察者才会开始发送事件. 也就是调用了subscribe() 方法之后才开始发送事件.
不带任何参数的subscribe() 表示下游不关心任何事件,你上游尽管发你的数据去吧, 老子可不管你发什么.
Emitter
Emitter,用于发送事件,共有三个方法
onNext(T value)、onComplete()和onError(Throwable error);
发送事件需要满足一定的规则:
1.可以发送和接收无限个onNext
2.onComplete,onError之后的事件可以继续发送,但不再被接收
3.onComplete,onError可以不被发送
4.onComplete,onError是唯一而且互斥的.
5.发送多个onError报错,发送多个onComplete只有第一个生效
Disposable
Disposable,可以理解为一个开关
Disposable.dispose()会把观察者和被观察者的联系切断,被观察者可以继续发送事件,但观察者无法继续接收事件
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
emitter.onNext(1);
emitter.onNext(2);
emitter.onNext(3);
emitter.onNext(4);
emitter.onNext(5);
emitter.onComplete();
}
}).subscribe(new Observer<Integer>() {
Disposable mDisposable;
@Override
public void onSubscribe(Disposable d) {
mDisposable=d;
}
@Override
public void onNext(Integer value) {
if (value==3){
mDisposable.dispose();
}
Log.d("meee", getClass() + ":\n" + "value:" + value);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
11-09 15:04:27.323 8834-8834/? D/meee: class com.junx.rxjava.RxJavaActivity$3:
value:1
11-09 15:04:27.323 8834-8834/? D/meee: class com.junx.rxjava.RxJavaActivity$3:
value:2
11-09 15:04:27.323 8834-8834/? D/meee: class com.junx.rxjava.RxJavaActivity$3:
value:3
Consumer
观察者之一,只关心onNext()事件
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
emitter.onNext(1);
emitter.onNext(2);
emitter.onNext(3);
emitter.onNext(4);
emitter.onNext(5);
emitter.onComplete();
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.d("meee",getClass()+":\n"+"integer:"+integer);
}
});
线程控制
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> e) throws Exception {
e.onNext("你好")
Log.d("meee",getClass()+"subscribe:\n"+Thread.currentThread().getName())
}
}) //创建一个线程发布事件
.subscribeOn(Schedulers.newThread())
//在主线程中接收(观察)事件
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.d("meee",getClass()+"accept:\n"+Thread.currentThread().getName())
}
})
//--------------------------------------日志-------------------------------------
11-09 15:15:26.380 22897-22921/? D/meee: class com.junx.rxjava.RxJavaActivity$2subscribe:
RxNewThreadScheduler-1
11-09 15:15:26.394 22897-22897/? D/meee: class com.junx.rxjava.RxJavaActivity$1accept:
main
接收事件的线程可以切换多次
//多次切换线程
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> e) throws Exception {
e.onNext("你好")
Log.d("meee", getClass() + "subscribe:\n" + Thread.currentThread().getName())
}
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.d("meee",getClass()+":\n"+s+":"+Thread.currentThread().getName())
}
})
.observeOn(Schedulers.io())
.subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.d("meee",getClass()+":\n"+s+":"+Thread.currentThread().getName())
}
})
//------------------------------------------------------------------------
11-09 15:21:14.082 30304-30334/com.junx.rxjava D/meee: class com.junx.rxjava.RxJavaActivity$3subscribe:
RxNewThreadScheduler-1
11-09 15:21:14.100 30304-30304/com.junx.rxjava D/meee: class com.junx.rxjava.RxJavaActivity$2:
你好:main
11-09 15:21:14.101 30304-30336/com.junx.rxjava D/meee: class com.junx.rxjava.RxJavaActivity$1:
你好:RxCachedThreadScheduler-1
线程介绍
Schedulers.io() 代表io操作的线程, 通常用于网络,读写文件等io密集型的操作
Schedulers.computation() 代表CPU计算密集型的操作, 例如需要大量计算的操作
Schedulers.newThread() 代表一个常规的新线程
AndroidSchedulers.mainThread() 代表Android的主线程
Rxjava与Retrofit的联合使用