use of io.reactivex.ObservableOnSubscribe in project BestPracticeApp by pop1234o.
the class RxJavaSample method observable.
// endregion
// region 创建操作
// https://github.com/ReactiveX/RxJava/wiki/Creating-Observables (各种创建操作符示例)
/**
* https://github.com/ReactiveX/RxJava/wiki/Creating-Observables (各种创建操作符示例)
* create
* defer
* empty
* error
* from fromIterable - 遍历collection
* generate
* interval
* just
* never
* range
* timer
*/
private void observable() {
ObservableJust.just("").map(new Function<String, Integer>() {
@Override
public Integer apply(String s) throws Exception {
return null;
}
}).subscribeOn(Schedulers.computation()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
}
});
// ==========================================
Observable.create(new ObservableOnSubscribe<String>() {
// 当有观察者订阅了我的(Observable)消息
@Override
public void subscribe(ObservableEmitter<String> e) throws Exception {
// 这里是被观察者
e.onNext("1");
e.onNext("2");
if (System.currentTimeMillis() > 0) {
// 只能调用一次,而且下面的语句不再被接收了
e.onError(new NullPointerException());
}
e.onComplete();
// onComplete后,不会执行了
e.onNext(":");
// 这里执行的线程是 subscribeOn指定的
}
}).subscribeOn(Schedulers.io()).observeOn(Schedulers.computation()).map(new Function<String, Integer>() {
@Override
public Integer apply(String s) throws Exception {
// 这里指定的线程是 Schedulers.computation()
return Integer.parseInt(s);
}
}).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Integer>() {
private Disposable d;
@Override
public void onSubscribe(Disposable d) {
// Disposable相当于事件对象,用于取消订阅关系???
this.d = d;
}
@Override
public void onNext(Integer s) {
// 这个线程是最近一个observeOn指定的
if ("compelete".equals(s)) {
// 中断事件流
d.dispose();
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
io.reactivex.Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> e) throws Exception {
e.onNext(1);
e.onNext(1);
e.onNext(1);
e.onComplete();
e.onNext(1);
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
// 这里只接受onNext中的事件,onComplete后的事件不会接收到
}
});
// 创建List发射器
Observable.fromIterable(new ArrayList<String>()).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
}
});
}
use of io.reactivex.ObservableOnSubscribe in project MyJapanese by 54wall.
the class FavWordsFragmentModelImpl method getData.
@Override
public void getData(Consumer<List<Word>> consumer, Consumer<Throwable> throwable, final String lessonId) {
Disposable disposable = Observable.create(new ObservableOnSubscribe<List<Word>>() {
@Override
public void subscribe(ObservableEmitter<List<Word>> emitter) throws Exception {
List<Word> list;
list = DBManager.getInstance().getFav(lessonId);
if (list == null) {
Log.e(TAG, "list == null");
// emitter.onError(new Exception());//经常报这个错误io.reactivex.exceptions.OnErrorNotImplementedException
} else {
emitter.onNext(list);
}
emitter.onComplete();
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(consumer, throwable);
mCompositeDisposable.add(disposable);
}
use of io.reactivex.ObservableOnSubscribe in project MyJapanese by 54wall.
the class GojuonFragmentModelImpl method getData.
@Override
public void getData(final int category, Consumer<List<GojuonItem>> consumer) {
Disposable disposable = Observable.create(new ObservableOnSubscribe<List<GojuonItem>>() {
@Override
public void subscribe(ObservableEmitter<List<GojuonItem>> emitter) throws Exception {
List<GojuonItem> list;
switch(category) {
case Constants.CATEGORY_QINGYIN:
list = DBManager.getInstance().getQingYin();
break;
case Constants.CATEGORY_ZHUOYIN:
list = DBManager.getInstance().getZhuoYin();
break;
case Constants.CATEGORY_AOYIN:
list = DBManager.getInstance().getAoYin();
break;
default:
list = null;
break;
}
if (list == null) {
emitter.onError(new Exception());
} else {
emitter.onNext(list);
}
emitter.onComplete();
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(consumer);
mCompositeDisposable.add(disposable);
}
use of io.reactivex.ObservableOnSubscribe in project LiYuJapanese by 54wall.
the class PixivIllustFragmentModelImpl method getIllusts.
@Override
public void getIllusts(final String mode, Consumer<List<PixivIllustBean>> consumer) {
Disposable disposable = Observable.create(new ObservableOnSubscribe<List<PixivIllustBean>>() {
@Override
public void subscribe(final ObservableEmitter<List<PixivIllustBean>> emitter) throws Exception {
service.getIllusts(mode, new Consumer<ResponseBody>() {
@Override
public void accept(ResponseBody responseBody) throws Exception {
List<PixivIllustBean> list = handleResponseBody(responseBody);
if (list == null) {
emitter.onError(new Exception());
} else {
emitter.onNext(list);
}
}
});
emitter.onComplete();
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(consumer);
mCompositeDisposable.add(disposable);
}
use of io.reactivex.ObservableOnSubscribe in project ManPinAPP by AnyMarvel.
the class ErrorAnalyContentManager method writeMayByNetError.
public void writeMayByNetError(final String url) {
Observable.create(new ObservableOnSubscribe<Boolean>() {
@Override
public void subscribe(ObservableEmitter<Boolean> e) throws Exception {
String filePath = MyApplication.getInstance().getExternalFilesDir("").getPath();
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(filePath, "ErrorNetUrl.txt");
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fileOutputStream2 = new FileOutputStream(file, true);
fileOutputStream2.write((url + " \r\n").getBytes());
fileOutputStream2.flush();
fileOutputStream2.close();
e.onNext(true);
e.onComplete();
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new SimpleObserver<Boolean>() {
@Override
public void onNext(Boolean value) {
}
@Override
public void onError(Throwable e) {
}
});
}
Aggregations