2016-05-25 4 views
3

iOS/Swift(RxSwift)でReactiveXを使用しています。ReactiveX観測を一時停止する方法

のは、私が観測を持っているとしましょう:

私が加入してい
let dataUpdates = ... 

:私はアニメーションてる間、私が更新を受け取った場合は、私が受信したくない

dataUpdates.subscribeNext({ data in 
    // update tableView with data 
    // maybe move to a difference cell with an animation 
}) 

アニメーションが終了するまで次の更新を行います(私はアニメーション中に起こった更新を緩和したくありません)。

私が必要とするのは、それを放つことを中止することです。それは、dataUpdatesが観察可能です。

どうすればこの問題を解決できますか?

+0

この質問は古いですが、多分あなたはまだそれに答えを見つけることができます。 Observableが(特定の閾値を超えて排出量を下げる以外の方法で)バックプレッシャーをサポートしていない限り、その観測に入ることから排出を単に止めることはできません。リアクションプログラミングは、プル機構)。 – MatBos

+0

私の問題を解決した 'subscribeNextAndWait'を実装しました。 –

答えて

0

BehaviorSubjectを使用して、更新を一時停止して再開するバルブを作成します。バルブが閉じられたときに到着する更新については、dataUpdatesに背圧サポート(すなわち、バッファリング)が必要になることに注意してください。だから、擬似コードで

(私はコードswitftをしないので、私の構文を言い訳)

// create a BehaviorSubject with default value true. It will always emit 
// the latest value when subscribed to, thus it is kind of a variable 
let valve = BehaviorSubject(value: true) 

// we are only interested to get one `true` value. When the latest value 
// received by the valve is `true`, this will give a value immediately when 
// subscribed to. When the latest value is `false`, this will not give 
// any events until it is true. 
let openValve = valve.filter{$0}.take(1) 

// for each data update, pass it through if the valve is open and otherwise 
// start waiting for it to turn true 
let pauseableDataUpdates = dataUpdates.concatMap{update in openValve.map { _ in update}} 

// now when you start rendering, you do 
valve.on(.Next(false)) 

// and after animation is done, you do 
valve.on(.Next(true)) 
関連する問題