RxJava
Observable:
被观察者
Observer:
观察者
subscribe:
订阅
observeOn():
事件回调线程
subscribeOn():
事件执行线程
第一种创建方式
// 创建被观察者
Observable novel = Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(ObservableEmitter emitter) throws Exception {
emitter.onNext("连载1");
emitter.onNext("连载2");
emitter.onNext("连载3");
emitter.onComplete();
}
});
// 创建观察者
Observer<String> reader = new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Log.d("WuMeng","onSubscribe");
}
@Override
public void onNext(String s) {
Log.d("WuMeng",s);
}
@Override
public void onError(Throwable e) {
Log.d("WuMeng","onError");
}
@Override
public void onComplete() {
Log.d("WuMeng","onComplete");
}
};
novel.subscribe(reader);
第二种创建方式
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
}
}).observeOn(AndroidSchedulers.mainThread()) // 回调在主线程
.subscribeOn(Schedulers.io()) // 执行在io线程
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String s) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});