2017-01-01 90 views
1

所以我現在有一個Observable,它返回用戶:獲得可觀察到發射兩種

public Observable<Account> getLoggedUserInfo() { 


    try { 
     return accountService.me(preferencesHelper.getToken()).concatMap(remoteAccount -> { 
      return localDatabase.addAccount(remoteAccount); 
     }); 

    } catch (NullPointerException e) { 

     return Observable.empty(); 

    } 


} 

而且我有一個Observable,返回學校的基礎上,ID,它看起來像這樣:

public Observable<School> getSchoolById(@NonNull final String id){ 

    return schoolService.getSchool(id).concatMap(remoteSchool->localDatabase.addSchool(remoteSchool)); 

} 

現在,我要代表我的觀點了學校,但爲了做到這一點,我需要創造一個Observable這會給我,我開始與帳戶(ObservablegetLoggedUserInfo)和SCH OOL它之後發生的事情,所以我會就要結束了類似

Observable<Account, School>{ 
//Do something with the account and school 

} 

那麼,如何獲得這個Observable,與我目前擁有的觀測,是它甚至可能嗎? 我對Rx還是比較新的,讓我困惑的很大一部分是語法,在此先感謝!

回答

2

使用元組或中介類。

public Observable<Tuple<<Account, School>> doSomething() { 
    return getLoggedUserInfo().zip(getSchoolById("id"), (f, s) -> new Tuple(x, y)); 
} 

我推薦你使用正確的元組,但它可以是簡單:

public class Tuple<X, Y> { 
    public final X x; 
    public final Y y; 
    public Tuple(X x, Y y) { 
    this.x = x; 
    this.y = y; 
    } 
}