首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ReactiveX如何暂停可观察对象

ReactiveX如何暂停可观察对象
EN

Stack Overflow用户
提问于 2016-05-26 02:59:39
回答 1查看 1.5K关注 0票数 4

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

假设我有一个观察值:

代码语言:javascript
复制
let dataUpdates = ...

我已经订阅了:

代码语言:javascript
复制
dataUpdates.subscribeNext({ data in
    // update tableView with data
    // maybe move to a difference cell with an animation
})

如果我在动画中收到更新,我不想在动画结束之前收到下一次更新(我也不想丢失动画过程中发生的更新)。

所以我需要做的就是暂停dataUpdates的发射。

我如何才能做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2017-06-04 15:41:26

使用BehaviorSubject创建暂停和恢复更新的阀。请注意,当阀门关闭时,您将需要在dataUpdates中提供一些背压支持(即缓冲),以便进行更新。

所以在伪代码中(我没有编写switft代码,所以请原谅我的语法)

代码语言:javascript
复制
// 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))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37445529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档