use of io.reactivex.ObservableEmitter in project LiYuJapanese by 54wall.
the class WordsFragmentModelImpl method getData.
@Override
public void getData(Consumer<List<Word>> consumer, final String lesson) {
Disposable disposable = Observable.create(new ObservableOnSubscribe<List<Word>>() {
@Override
public void subscribe(ObservableEmitter<List<Word>> emitter) throws Exception {
List<Word> list = DBManager.getInstance().queryWord(lesson);
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.ObservableEmitter in project LiYuJapanese 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.ObservableEmitter in project LiYuJapanese by 54wall.
the class GojuonMemoryFragmentModelImpl method getData.
@Override
public void getData(final int category, Consumer<List<GojuonMemory>> consumer) {
Disposable disposable = Observable.create(new ObservableOnSubscribe<List<GojuonMemory>>() {
@Override
public void subscribe(ObservableEmitter<List<GojuonMemory>> emitter) throws Exception {
List<GojuonMemory> list;
switch(category) {
case Constants.GOJUON_MEMORY:
list = DBManager.getInstance().getGojuonMemory();
break;
case Constants.GOJUON_CHENGYU:
list = DBManager.getInstance().getGojuonChengyu();
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.ObservableEmitter in project MVP by yuchengren.
the class RxAndroidActivity method test2.
private void test2() {
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> e) throws Exception {
// IO线程中做网络访问等耗时操作
// String responseJsonString = OkHttpUtil.get("http://192.168.0.1:8080/TradeType=getPeoples");
// String responseJsonString = "[{\"gendar\":29,\"name\":\"ren\",\"sex\":0},{\"gendar\":30,\"name\":\"ling\",\"sex\":1}]";
List<People> peopleList = new ArrayList<>();
peopleList.add(new People("ren", 0, 29));
peopleList.add(new People("ling", 1, 30));
LogHelper.e(TAG, GsonUtil.formatObjectToJson(peopleList));
e.onNext(GsonUtil.formatObjectToJson(peopleList));
}
}).map(new Function<String, List<People>>() {
@Override
public List<People> apply(String s) throws Exception {
LogHelper.d(TAG, "map,CurrentThreadName=" + Thread.currentThread().getName());
List<People> peopleList = GsonUtil.parseJsonToList(s, People.class);
// json解析成实体类
return peopleList;
}
}).doOnNext(new Consumer<List<People>>() {
@Override
public void accept(List<People> people) throws Exception {
LogHelper.d(TAG, "doOnNext,CurrentThreadName=" + Thread.currentThread().getName());
// 存入数据库
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<List<People>>() {
@Override
public void accept(List<People> people) throws Exception {
// 主线程中刷新页面
}
});
}
use of io.reactivex.ObservableEmitter in project RxDownload by ssseasonnn.
the class DownloadService method startDispatch.
/**
* start dispatch download queue.
*/
private void startDispatch() {
disposable = Observable.create(new ObservableOnSubscribe<DownloadMission>() {
@Override
public void subscribe(ObservableEmitter<DownloadMission> emitter) throws Exception {
DownloadMission mission;
while (!emitter.isDisposed()) {
try {
log(WAITING_FOR_MISSION_COME);
mission = downloadQueue.take();
log(Constant.MISSION_COMING);
} catch (InterruptedException e) {
log("Interrupt blocking queue.");
continue;
}
emitter.onNext(mission);
}
emitter.onComplete();
}
}).subscribeOn(Schedulers.newThread()).subscribe(new Consumer<DownloadMission>() {
@Override
public void accept(DownloadMission mission) throws Exception {
mission.start(semaphore);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
log(throwable);
}
});
}
Aggregations