2016-08-12 49 views
2

我試圖學習RxAndroi,所以問題就像在標題上。RxAndroid - 1來自多個觀察對象的響應

1響應從多個觀測量

我想從這個2個請求響應1。

或選項編號2我也想知道如何同步它們。

第一次完成後再做第二次請求。

示例代碼:

Observable<List<CategoriesTreeModel>> categoriesTreeObservable = ApiManager.getInstanceApi().getCategoriesTree(); 
     Subscription treeSubscription = categoriesTreeObservable 
       .subscribeOn(Schedulers.io()) 
       .observeOn(AndroidSchedulers.mainThread()) 
       .subscribe(new Subscriber<List<CategoriesTreeModel>>() { 
        @Override 
        public void onCompleted() { 
         Log.e("CATALOG", "getCategoriesTree() complete"); 
        } 

        @Override 
        public void onError(Throwable e) { 
         Log.e("CATALOG", "getCategoriesTree() error"); 
        } 

        @Override 
        public void onNext(List<CategoriesTreeModel> categoriesTreeModels) { 
         Log.e("CATALOG", "getCategoriesTree() next"); 
        } 
       }); 



     Observable<ResponseModel<CategoriesResponse>> categoriesObservable = ApiManager.getInstanceApi().getCategories(); 
     Subscription subscription = categoriesObservable 
       .subscribeOn(Schedulers.io()) 
       .observeOn(AndroidSchedulers.mainThread()) 
       .subscribe(new Subscriber<ResponseModel<CategoriesResponse>>() { 
        @Override 
        public void onCompleted() { 
         Log.e("CATALOG", "getCategories() complete"); 

        } 

        @Override 
        public void onError(Throwable e) { 
         Log.e("CATALOG", "getCategories() error"); 
        } 

        @Override 
        public void onNext(ResponseModel<CategoriesResponse> categoriesResponseResponseModel) { 
         Log.e("CATALOG", "getCategories() onNext"); 


        } 
       }); 

編輯: 這是一個正確的whay?

Observable.zip(categoriesObservable, categoriesTreeObservable, new Func2<ResponseModel<CategoriesResponse>, List<CategoriesTreeModel>, Object>() { 
      @Override 
      public Object call(ResponseModel<CategoriesResponse> categoriesResponseResponseModel, List<CategoriesTreeModel> categoriesTreeModels) { 

       for (int i = 0; i < categoriesResponseResponseModel.getList().size(); i++){ 
        Log.e("RESPONSE", "CATEGORIES: " + categoriesResponseResponseModel.getList().get(i).getCategory_id() + " NAME: " + categoriesResponseResponseModel.getList().get(i).getTranslations().getPl_PL().getName()); 
       } 

       for (int i = 0; i < categoriesTreeModels.size(); i++){ 
        Log.e("RESPONSE", "TREE: ID " + categoriesTreeModels.get(i).getId() + " CHILD: " + (categoriesTreeModels.get(i).getChildren().size() > 0 ? " has children " : "no child")); 
       } 

       return null; 
      } 
     }) 
       .subscribeOn(Schedulers.newThread()) 
       .observeOn(AndroidSchedulers.mainThread()) 
       .subscribe(); 
+1

看看拉鍊operaor http://stackoverflow.com/questions/30219877/rxjava-android-how-to-use-the-zip-operator,做這裏看看https://開頭github.com/ReactiveX/RxJava/wiki/Combining-Observables – Raghunandan

+0

它看起來不錯,但你需要把它們結合在一起返回一個調用結果,這樣,當你訂閱你獲得的物品在onNext – Raghunandan

回答

-1

閱讀關於MVP模式的內容,以便將數據邏輯從視圖邏輯中分離出來。 最簡單的解決方案是「先完成後再做第二次請求」。如下所示。這種模式並不是世界上最好的,但至少它會給你一些你可以參考的代碼。

final Observable<String> firstObservable = Observable.just("A") 
     .delay(5, TimeUnit.SECONDS) 
     .doOnNext(new Action1<String>() { 
      @Override 
      public void call(String s) { 
       Log.d(TAG, "call: " + s); 
      } 
     }); 

firstObservable 
     .subscribe(); 

final Observable<String> secondObservable = firstObservable 
     .flatMap(new Func1<String, Observable<String>>() { 
      @Override 
      public Observable<String> call(String s) { 
       return Observable.just(s + "B").delay(2, TimeUnit.SECONDS); // Make some api call 
      } 
     }); 

secondObservable.subscribe(new Action1<String>() { 
    @Override 
    public void call(String s) { 
     Log.d(TAG, "call: " + s); 
    } 
});