RXjava具体使用(自己的笔记)

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() {
    @Override
    public void call(Subscribersuper Integer> subscriber) {
        subscriber.onNext(1);
       /* subscriber.onCompleted();*/
    }
});
MySubscriba = new Subscriber() {
    @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);

你可能感兴趣的:(android)