RXjava具體使用(自己的筆記)
阿新 • • 發佈:2018-12-29
1、MyObservable.subscribeOn(Schedulers.io()) // 指定主執行緒 .observeOn(AndroidSchedulers.mainThread()).subscribe(MySubscriber);
這裡的AndroidSchedulers.mainThread()報:
observeOn | (rx.Scheduler) | in Observable cannot be applied |
to | (io.reactivex.Scheduler) |
compile 'io.reactivex:rxandroid:1.2.1' compile 'io.reactivex:rxjava:1.2.3'
2、如果有重複用到某一個非同步,請不要在他
subscriber.onNext(msg);時候
subscriber.onCompleted();否則只能作用一次
3、在Ontext裡面UI操作相關的,都要指定
.observeOn(AndroidSchedulers.mainThread())//在主執行緒中操作
一個完整的流程
myObservable2 = Observable.create(new Observable.OnSubscribe<Integer>() { @Override public void call(Subscriber<? super Integer> subscriber) { subscriber.onNext(1); /* subscriber.onCompleted();*/ } });
MySubscriba = new Subscriber<Integer>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Integer integer) { if (integer == 1) { TbLog.printI("lognast", integer + ""); if (time > 0) { text.setText("驗證碼已傳送" + time + "秒"); time--; TbLog.printI("log", time + ""); try { Thread.sleep(1000); reminderText2();//執行定閱的操作 } catch (InterruptedException e) { e.printStackTrace(); } } else { text.setText("提示資訊"); time = 60; text.setVisibility(View.GONE); getyanzheng.setVisibility(View.VISIBLE); } } else { cord.setText(""); text.setText("提示資訊"); time = 60; text.setVisibility(View.GONE); getyanzheng.setVisibility(View.VISIBLE); } } };
myObservable2.subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(MySubscriba);