2017-07-29 94 views
0

我是reactiveX的新手,所以這可能是一個愚蠢的問題。 我想在一個方法調用中一個接一個地結合兩個操作。RX java組合邏輯

步驟1是獲取數據的某些細節,並存儲那些細節

步驟2是獲取基於這些細節的數據。

public Observable<ApiResponse> getData(String id, String token) 
{ 
    //step 1 
    apiInterface.fetchDataDetails(id) 
      .map(new Func1<Map<String, String>, Void>() 
      { 
       @Override 
       public Void call(Map<String, String> details) 
       { 
        if (details != null && !details.isEmpty() && 
          details.containsKey("version") && details.containsKey("count")) 
        { 
         // save details 
        } else 
         throw new RuntimeException("Details not present"); 
        return null; 
       } 
      }); 



    //step 2 if step 1 is complete i.e data details are saved 
    //then bring data 

    int dataCount = 5; // the count saved from fetched details 
    List<Observable> dataCalls = new ArrayList<>(); 
    for (int i = 0; i < dataCount; i++) 
dataCalls.add(apiInterface.fetchData(token,"http://blahblahblah/data/"+i)); 

    return Observable.zip(dataCalls, new FuncN<ApiResponse>() 
    { 
     @Override 
     public ApiResponse call(Object... args) 
     { 
      // read all the data from all the observable, 
      //combine with some logic 
      // and then return just one ApiResponse. 
      return new ApiResponse(); 
     } 
    }); 
} 

什麼我不能夠理解的是,如何合併僅如果第1步成功完成步驟1和2 第二步應該只執行。

我想過這個。

public Observable<ApiResponse> getData(String id, String token) 
{ 
    return Observable.create(new Observable.OnSubscribe<ApiResponse>() 
     { 
     @Override 
     public void call(Subscriber<? super ApiResponse> subscriber) 
     { 
      apiInterface.fetchDataDetails(id) 
         .map(new Func1<Map<String, String>, Void>() 
         { 
          @Override 
          public Void call(Map<String, String> details) 
          { 
           if (details != null && !details.isEmpty() && 
             details.containsKey("version") && 
             details.containsKey("count")) 
           { 
            // save details 
           } else 
            throw new RuntimeException("Details not 
                  present"); 
           return null; 
          } 
         }).doOnCompleted(new Action0() 
          { 
           @Override 
           public void call() 
           { 
            int dataCount = 5; // the count saved 
                 //from fetched details 
            List<Observable> dataCalls = new 
                 ArrayList<>(); 
            for (int i = 0; i < dataCount; i++) 
            dataCalls.add(apiInterface 
               .fetchData(token, 
             "http://blahblahblah/data/"+i)); 

            return Observable.zip(dataCalls, new 
            FuncN<ApiResponse>() 
            { 
            @Override 
            public ApiResponse call(Object... args) 
            { 
             // read all the data from all the 
             //observable, combine with some logic 
             // and then return just one 
             //ApiResponse. 
             return new ApiResponse(); 
            } 
            }); 
           } 
          }); 
      } 
     }); 
    } 

但是這樣我就不能發回我的數據了,這感覺就像一個反模式。

回答