我在iOS / Swift(ReactiveX)中使用RxSwift。
假设我有一个可观察的:
let dataUpdates = ...
我订阅了:
dataUpdates.subscribeNext({ data in
// update tableView with data
// maybe move to a difference cell with an animation
})
如果我在制作动画时收到更新,我不希望在动画结束前收到下一次更新(我不想放弃动画期间发生的更新)。
所以我需要的是暂停dataUpdates
可观察的发光。
我怎样才能做到这一点?
答案 0 :(得分:1)
使用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))