Search in sources :

Example 21 with ObservableEmitter

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);
}
Also used : Disposable(io.reactivex.disposables.Disposable) Word(pri.weiqiang.liyujapanese.mvp.bean.dic.Word) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 22 with ObservableEmitter

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);
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) GojuonItem(pri.weiqiang.liyujapanese.mvp.bean.gojuon.GojuonItem) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 23 with ObservableEmitter

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);
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) GojuonMemory(pri.weiqiang.liyujapanese.mvp.bean.gojuon.GojuonMemory) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 24 with ObservableEmitter

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 {
        // 主线程中刷新页面
        }
    });
}
Also used : ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) Consumer(io.reactivex.functions.Consumer) People(com.ycr.module.base.entity.People) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 25 with ObservableEmitter

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);
        }
    });
}
Also used : ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) Consumer(io.reactivex.functions.Consumer) DownloadMission(zlc.season.rxdownload2.entity.DownloadMission) ObservableEmitter(io.reactivex.ObservableEmitter)

Aggregations

ObservableEmitter (io.reactivex.ObservableEmitter)39 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)38 Disposable (io.reactivex.disposables.Disposable)24 IOException (java.io.IOException)7 Observable (io.reactivex.Observable)6 NonNull (io.reactivex.annotations.NonNull)6 File (java.io.File)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Intent (android.content.Intent)4 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 Consumer (io.reactivex.functions.Consumer)4 Word (pri.weiqiang.liyujapanese.mvp.bean.dic.Word)4 Observer (io.reactivex.Observer)3 Bitmap (android.graphics.Bitmap)2 Uri (android.net.Uri)2 NonNull (android.support.annotation.NonNull)2 OnClick (butterknife.OnClick)2 Progress (com.lzy.okgo.model.Progress)2 FileNotFoundException (java.io.FileNotFoundException)2