1. 程式人生 > >RxJava配合Retrofit2使用之Observable.zip

RxJava配合Retrofit2使用之Observable.zip

看大神的demo記錄分享一下,直接上程式碼

//通用的Rx執行緒轉換類

public class RxSchedulers {


    static final ObservableTransformer schedulersTransformer = new ObservableTransformer() {
        @Override
        public ObservableSource apply(Observable upstream) {
            return (upstream).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
        }
    };


    public static <T> ObservableTransformer<T, T> applySchedulers() {
        return (ObservableTransformer<T, T>) schedulersTransformer;
    }
}
  public void loadHomeData() {
        mView.showLoading();
        String username = SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.USERNAME_KEY);
        String password = SPUtils.getInstance(Constant.SHARED_NAME).getString(Constant.PASSWORD_KEY);
        //登入請求的被觀察者
        Observable<DataResponse<User>> observableUser = RetrofitManager.create(ApiService.class).login(username, password);
        //請求banners資料的被觀察者
        Observable<DataResponse<List<Banner>>> observableBanner = RetrofitManager.create(ApiService.class).getHomeBanners();
        //請求列表資料的被觀察者
        Observable<DataResponse<Article>> observableArticle = RetrofitManager.create(ApiService.class).getHomeArticles(mPage);
        //併發請求3個,等待全部結果返回後在處理
        Observable.zip(observableUser, observableBanner, observableArticle, new Function3<DataResponse<User>, DataResponse<List<Banner>>, DataResponse<Article>, Map<String, Object>>() {
            @Override
            public Map<String, Object> apply(DataResponse<User> response, DataResponse<List<Banner>> dataResponse, DataResponse<Article> dataResponse2) throws Exception {
                //請求完成對3個結果進行組裝
                Map<String, Object> objMap = new HashMap<>();
                objMap.put(Constant.USER_KEY, response);
                objMap.put(Constant.BANNER_KEY, dataResponse.getData());
                objMap.put(Constant.ARTICLE_KEY, dataResponse2.getData());
                return objMap;
            }
        }).compose(RxSchedulers.<Map<String, Object>>applySchedulers())//設定執行執行緒
                .compose(mView.<Map<String, Object>>bindToLife())//繫結activity的生命週期
                .subscribe(new Consumer<Map<String, Object>>() {//請求成功處理
            @Override
            public void accept(Map<String, Object> map) throws Exception {
                DataResponse<User> dataResponse = (DataResponse<User>) map.get(Constant.USER_KEY);
                if (dataResponse.getErrorCode() == 0) {
                    mView.showSuccess(App.getAppContext().getString(R.string.auto_login_success));
                } else {
                    mView.showFaild(String.valueOf(dataResponse.getErrorMsg()));
                }
                List<Banner> banners = (List<Banner>) map.get(Constant.BANNER_KEY);
                Article article = (Article) map.get(Constant.ARTICLE_KEY);
                mView.setHomeBanners(banners);
                mView.setHomeArticles(article, LoadType.TYPE_REFRESH_SUCCESS);
            }
        }, new Consumer<Throwable>() {//請求失敗處理
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.showFaild(throwable.getMessage());
            }
        });
    }