2017-01-13 7 views
1

私のビューには3つのフィールドがあり、一緒に式を構成します。私が達成したいのは、ユーザーが3つのフィールドのうち2つを塗りつぶすとすぐに残りのフィールドが計算されます。私が持っているものY要素の最初のXのcombineLatest

:私はフィールドの各ペアのために試してみました

mObservableEditTextA = RxTextView.textChanges(mEditTextA); 
mObservableEditTextB = RxTextView.textChanges(mEditTextB); 
mObservableEditTextC = RxTextView.textChanges(mEditTextC); 

なしの成功と他の二つの組み合わせ最新の情報を取得します。

Observable.combineLatest(mObservableEditTextA, mObservableEditTextB, (a, b) -> /* My action here */); 
Observable.combineLatest(mObservableEditTextA, mObservableEditTextC, (a, c) -> /* My action here */); 
Observable.combineLatest(mObservableEditTextB, mObservableEditTextC, (b, c) -> /* My action here */); 

この動作はどのようにして達成できますか?

答えて

0

おそらく良い方法がありますが、各イベントに排出のタイムスタンプを付けて、どれが最後かを判断して残りを放出することができます。彼らが来た観測可能な(フィールド)でイベントを特定し、リストにない要素によって決定する。

mObservableEditTextA = RxTextView.textChanges(mEditTextA) 
    .debounce(500, TimeUnit.MILLISECONDS) // So we don't flood the heap with lots of object construction, creating performance overhead due to garbage collection 
    .map(text -> Pair.create(new Date(), text)); 
mObservableEditTextB = RxTextView.textChanges(mEditTextB) 
    .debounce(500, TimeUnit.MILLISECONDS) 
    .map(text -> Pair.create(new Date(), text)); 
mObservableEditTextC = RxTextView.textChanges(mEditTextC) 
    .debounce(500, TimeUnit.MILLISECONDS) 
    .map(text -> Pair.create(new Date(), text)); 

Observable.combineLatest(mObservableEditTextA, mObservableEditTextB, mObservableEditTextC, (fieldAPair, fieldBPair, fieldCPair) -> { 
     firstField = ...; 
     secondField = ...; 
     // from timestamps determine which of the fields emitted last and return the other two with identifiers 
     return Arrays.asList(Pair.create(firstFieldIdentifier, firstField), Pair.create(secondFieldIdentifier, secondField)); 
    }) 
    .subscribe(result -> { 
     /* result is always a list of 2 items, more specifically 
      pairs of an identification in first position and new text 
      in the second. 

      Here you can look for the missing field in the list and 
      compute it from the other two */ 
    }) 

ここでは、どちらを計算したいかを決定するロジックが複製されています。私は、新しいオブジェクトにそれらのオブジェクトをラップする必要がないという理由でそれを行い、ネストされたペアは可読性を失います。

ただし、フィールドの識別子としてリスト内の位置を扱うことはできますが、それは間違いがちです。

これらのアプローチのいずれも、決定ロジックをsubscriberオペレータとcombineLatestオペレータの両方から加入者自身に移動するだけです。あなたの電話。

関連する問題