ios – ReactiveX如何暂停observable

ios – ReactiveX如何暂停observable,第1张

概述我在iOS / Swift( RxSwift)中使用 ReactiveX. 假设我有一个可观察的: let dataUpdates = ... 我订阅的是: dataUpdates.subscribeNext({ data in // update tableView with data // maybe move to a difference cell with an anim 我在iOS / Swift( RxSwift)中使用 ReactiveX.

假设我有一个可观察的:

let dataUpdates = ...

我订阅的是:

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

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

所以我需要的是暂停发出dataUpdates的可观察性.

我怎样才能做到这一点?

解决方法 使用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 variablelet 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{}.take(1)// for each data update,pass it through if the valve is open and otherwise// start waiting for it to turn truelet pauseableDataUpdates = dataUpdates.concatMap{update in openValve.map { _ in update}}// Now when you start rendering,you dovalve.on(.Next(false))// and after animation is done,you dovalve.on(.Next(true))
总结

以上是内存溢出为你收集整理的ios – ReactiveX如何暂停observable全部内容,希望文章能够帮你解决ios – ReactiveX如何暂停observable所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1029757.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存