Search in sources :

Example 61 with ObservableOnSubscribe

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 {
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiFunction(io.reactivex.functions.BiFunction) Function(io.reactivex.functions.Function) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 62 with ObservableOnSubscribe

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

Example 63 with ObservableOnSubscribe

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

Example 64 with ObservableOnSubscribe

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);
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) ArrayList(java.util.ArrayList) List(java.util.List) PixivIllustBean(pri.weiqiang.liyujapanese.mvp.bean.pixivIllust.PixivIllustBean) ObservableEmitter(io.reactivex.ObservableEmitter) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Example 65 with ObservableOnSubscribe

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) {
        }
    });
}
Also used : ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ObservableEmitter(io.reactivex.ObservableEmitter)

Aggregations

ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)69 ObservableEmitter (io.reactivex.ObservableEmitter)57 Disposable (io.reactivex.disposables.Disposable)38 Observable (io.reactivex.Observable)15 File (java.io.File)13 ArrayList (java.util.ArrayList)12 List (java.util.List)12 Intent (android.content.Intent)10 NonNull (io.reactivex.annotations.NonNull)10 IOException (java.io.IOException)10 SupportSQLiteDatabase (androidx.sqlite.db.SupportSQLiteDatabase)9 Consumer (io.reactivex.functions.Consumer)8 Bitmap (android.graphics.Bitmap)7 Observer (io.reactivex.Observer)7 Uri (android.net.Uri)6 Bundle (android.os.Bundle)6 OnClick (butterknife.OnClick)6 Log (android.util.Log)5 View (android.view.View)5 BindView (butterknife.BindView)5