gpt4 book ai didi

ios - ReactiveX 如何暂停 observable

转载 作者:搜寻专家 更新时间:2023-10-31 19:33:59 27 4
gpt4 key购买 nike

我正在使用 ReactiveX在 iOS/Swift 中(RxSwift)。

假设我有一个可观察对象:

let dataUpdates = ...

我订阅的:

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

如果我在制作动画时收到更新,我不想在动画结束之前收到下一个更新(我也不想丢失动画期间发生的更新)。

所以我只需要暂停 dataUpdates observable 的发射。

我怎样才能做到这一点?

最佳答案

使用 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))

关于ios - ReactiveX 如何暂停 observable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37445529/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com