2017-02-27 86 views
1

要建立一個完整的User實體,我需要執行此任務:從userId 無法建立這種可觀察

    1. 得到一個UserResponse(REST API響應)從userId
    2. 得到一個 List<Role>
    3. 獲取從UserResponse.addressId
    4. 一個Address構建從UserResponseUserList<Role>Address

    我嘗試這樣做:

    public Observable<User> getUserById(String userId) {  
        return Observable.zip(getUserResponse(userId), getRoles(userId), new Func2<UserResponse, List<Role>, User>() { 
         @Override 
         public User call(UserResponse response, List<Role> roles) { 
          //I need the address here! 
         } 
    }); 
    

    但我不知道如何使用AddressRepository來從UserResponse.addressIdAddress

    public interface AddressRepository { 
        Observable<Address> getAddress(String addressId); 
    } 
    

    User構造是這樣的:

    public User(String userId, Address address, List<Role> roles) { ... } 
    

    我希望你能理解。很難解釋這個問題。

  • 回答

    2

    使用cache()操作:

    public Observable<User> getUserById(String userId) { 
        Observable<UserResponse> response = getUserResponse(userId).cache(); 
        return Observable.zip(
        response, 
        response.flatMap(new Func1<UserResponse, Observable<Address>>() { 
         @Override 
         public Observable<Address> call(UserResponse resp) { 
          return addressRepository.getAddress(resp.addressId); 
         } 
        }), 
        getRoles(userId), new Func3<UserResponse, Address, List<Role>, User>() { 
         @Override 
         public User call(UserResponse resp, Address ad, List<Role> roles) { 
          //Now, I have all I need to build the User =) 
         } 
        } 
        }); 
    } 
    
    +0

    但是你不使用上面的'cache()'操作符 –

    +0

    Whop,編輯和修復! –

    0

    我找到了一個解決方案:

    public Observable<User> getUserById(String userId) { 
        return Observable.zip(
         getUserResponse(userId), 
         getUserResponse(userId).flatMap(new Func1<UserResponse, Observable<Address>>() { 
          @Override 
          public Observable<Address> call(UserResponse resp) { 
           return addressRepository.getAddress(resp.addressId); 
          } 
         }), 
         getRoles(userId), new Func3<UserResponse, Address, List<Role>, User>() { 
          @Override 
          public User call(UserResponse resp, Address ad, List<Role> roles) { 
           //Now, I have all I need to build the User =) 
          } 
         } 
    }); 
    

    任何意見將不勝感激。 「重複」getUserResponse(userId)的呼叫是否是一種很好的做法?我沒有找到解決這個問題的另一種方法。

    +0

    getUserResponse(用戶ID).publish.autoConnect(2);將其設置爲ConnectableObservable,並且只會開始發佈2個訂閱者的位置,這樣您將只調用一次後端。 –

    1
    getUserResponse(userId).publish.autoConnect(2); 
    

    將使其ConnectableObservable只有開始出版哪裏有2個用戶,這樣你會一次調用後端。

    getUserAddress()將調用getUserResponse並從中獲取地址。

    現在拉鍊getUserResponse,getUserAddress和getRoles(用戶ID)一起:)

    1

    您可以使用flatMap一個變種這樣做

    public Observable<User> getUserById(String userId) { 
        return Observable.zip(
          getUserResponse(userId).flatMap(new Func1<UserResponse, Observable<Address>>() { 
          @Override 
          public Observable<Address> call(UserResponse resp) { 
           return addressRepository.getAddress(resp.addressId); 
          } 
          }, 
          new Func2<UserResponse, Address, Pair<UserResponse,Address>>() { 
          @Override 
          public Pair<UserResponse,Address> call(UserResponse resp, Address addr) { 
           return new Pair<UserResponse,Address>(resp,addressRepository.getAddress(resp.addressId)); 
          }  
          }), 
          getRoles(userId), 
          new Func2<Pair<UserResponse, Address>, List<Role>, User>(){ 
          @Override 
          public User call(UserResponse resp, Address ad, List<Role> roles) { 
          //Now, I have all I need to build the User 
          }    
          } 
         ); 
    }