1. 程式人生 > >rxjava 鏈式排程

rxjava 鏈式排程

Rxjava 真是的是好東西 隨手記錄下用過的鏈式排程 方便使用的時候直接修改


Observable.create(new ObservableOnSubscribe<String>() {
        @Override
        public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext(login());

                Log.e(TAG, "subscribe login:" + Thread.currentThread
().getName()); } }).map(new Function<String, String>() { @Override public String apply(String s) throws Exception { Log.e(TAG, "194 apply:" + Thread.currentThread().getName()); return "apply"; } }).map(new Function<String, String>() { @Override
public String apply(String s) throws Exception { Log.e(TAG, "194 apply:" + Thread.currentThread().getName()); return "apply"; } }).doOnNext(new Consumer<String>() { //儲存登入結果UserInfo @Override public void accept(@NonNull String bean) throws
Exception { Log.e(TAG, "1doOnNext:accept" + Thread.currentThread().getName()); saveUserInfo(bean); } }) .subscribeOn(Schedulers.io()) //排程執行緒 .observeOn(AndroidSchedulers.mainThread()) //排程執行緒 .subscribe(new Consumer<String>() { @Override public void accept(@NonNull String bean) throws Exception { //整個請求成功,根據獲取的UserInfo更新對應的View showSuccessView(bean); Log.e(TAG, "Consumer:accept" + Thread.currentThread().getName()); } }, new Consumer<Throwable>() { @Override public void accept(@NonNull Throwable throwable) throws Exception { //請求失敗,顯示對應的View Log.e(TAG, "222 Consumer:" + Thread.currentThread().getName()); showFailView(); } });