gpt4 book ai didi

swift - 为 UIPageViewController.setViewControllers 绑定(bind) RxCocoa(如何同步 Observable)

转载 作者:搜寻专家 更新时间:2023-11-01 05:33:14 24 4
gpt4 key购买 nike

目前,我想绑定(bind)Observable<UIViewController>的值到我的UIPageViewController .最初的想法简单明了:

viewControllerObservable
.subscribe(onNext: { [weak self] viewController in

self?.pageVC.setViewControllers([viewController],
direction: .forward,
animated: false,
completion: nil)
})
.disposed(by: disposeBag)

但是如果 viewControllerObservable 中的值变化太快,页面 View Controller 在添加以前的 View Controller 之前收到新的设置命令,我得到 NSInternalInconsistencyException .

我可以使用 Binder 使该代码更具“ react 性” ,但我仍然会遇到同样的问题。

这里很明显的方法是使用 throttle ,但它似乎是很脏的修复,掩盖问题,而不是修复它。

理想情况下,我需要一些方法来保存更多值处理直到 completionsetViewControllers 中调用.

我怎样才能做到这一点?

最佳答案

因此,到目前为止,我遇到的最佳解决方案是使用第二个可观察对象来实现同步。

let sync = BehaviorSubject<Void>(value: ())

Observable.zip(pageProvider.selectedViewControllerObservable, sync)
.subscribe(onNext: { [weak self] viewController, _ in
self?.pageVC.setViewControllers([viewController],
direction: .forward,
animated: false,
completion: {sync.onNext()})
})
.disposed(by: disposeBag)

Observable.zip 等待两个 observable 发出值时,它会被“锁定”直到 sync 收到下一个值。

关于swift - 为 UIPageViewController.setViewControllers 绑定(bind) RxCocoa(如何同步 Observable),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49756479/

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