ios - 使用 RXSwift 观察 contentSize 时检测到重入异常

标签 ios swift rx-swift

我对 RXSwift 的 TableView 进行了以下设置:

override func viewDidLoad() {
    super.viewDidLoad()

    // Constraints setup etc
    
    items
        .subscribeOn(MainScheduler.instance)
        .bind(to: tableView.rx.items(cellType: SomeCell.self)) { [weak self] row, item, cell in
            // Code
        }
        .disposed(by: bag)

    // Observe the contentSize of tableView to update PanModal's height.
    tableView.rx.observe(CGRect.self, "contentSize")
        .subscribeOn(MainScheduler.instance)
        .subscribe(onNext: { [weak self] _ in
            // Code
        })
        .disposed(by: bag)
}

似乎是因为我使用 RXSwift 订阅了 contentSize 我现在收到以下错误:

⚠️ Reentrancy anomaly was detected. Debugging: To debug this issue you can set a breakpoint in /Users/kekearif/Documents/Snapask/ios-app/Pods/RxSwift/RxSwift/Rx.swift:96 and observe the call stack. Problem: This behavior is breaking the observable sequence grammar. next (error | completed)? This behavior breaks the grammar because there is overlapping between sequence events. Observable sequence is trying to send an event before sending of previous event has finished. Interpretation: This could mean that there is some kind of unexpected cyclic dependency in your code, or that the system is not behaving in the expected way. Remedy: If this is the expected behavior this message can be suppressed by adding .observeOn(MainScheduler.asyncInstance) or by enqueuing sequence events in some other way.

有没有人见过这个?任何想法可能导致此错误的原因是什么?我尝试按照它的建议添加 .observeOn(MainScheduler.asyncInstance),当我显示 View Controller 时,UI 就锁定了。

如有任何关于如何解决此问题的建议,我们将不胜感激!

最佳答案

不是因为您订阅了contentSize,请注意您发布的代码不会导致异常;这是因为您在订阅(或绑定(bind))中所做的事情导致了问题的发生。

如您收到的警告中所述,“可观察序列正在尝试在上一个事件发送完成之前发送一个事件。”这通常是由程序员在绑定(bind)到主题的订阅内的主题上发送 onNext 事件引起的。在这种情况下,您可能会从订阅内部更改 TableView 的 contentSize。解决方案是不要这样做。

详细解释您认为需要这样做的原因,也许我可以向您展示更好的方法。

关于ios - 使用 RXSwift 观察 contentSize 时检测到重入异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65047776/

相关文章:

ios - 如何根据实现该协议(protocol)的两个实例的身份为协议(protocol)实现 Equatable 协议(protocol)?

ios - 启用带有 RxSwift 逻辑的按钮

ios - 如何使用 ReactiveX 按顺序执行异步

ios - 如何对 rxSwift 变量进行串行执行?

ios - 如何获取 UIPickerView 的文本?

ios - 在打开 UIViewController 时展开可选值时意外发现 nil

ios - 是否可以通过在 iOS swift 中画线来对图像进行分组?

iphone - 如何在 iOS 中使用 jar

ios - 在 NSDate 对象中仅存储时区独立时间

ios - 将 UICollectionViewCell 放入 UICollectionView