简体   繁体   English

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

[英]Reentrancy anomaly was detected when observing contentSize with RXSwift

I have the following setup for a table view with RXSwift:对于 RXSwift 的表视图,我有以下设置:

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)
}

It seems that because I am subscribed to contentSize with RXSwift I now get the following error:似乎因为我使用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.调试:要调试此问题,您可以在 /Users/kekearif/Documents/Snapask/ios-app/Pods/RxSwift/RxSwift/Rx.swift:96 中设置断点并观察调用堆栈。 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)或以其他方式将序列事件排入队列来抑制此消息。

Has anyone seen this before?有没有人见过这个? Any ideas what might be causing this error?任何想法可能导致此错误? I tried adding .observeOn(MainScheduler.asyncInstance) as it suggests and the UI just locks up when I present the view controller.我尝试按照它的建议添加.observeOn(MainScheduler.asyncInstance) ,当我呈现视图 controller 时,UI 就会锁定。

Any suggestions on how to fix this would be much appreciated!任何有关如何解决此问题的建议将不胜感激!

It's not because you subscribed to contentSize , note that the code you posted doesn't cause the anomaly;这不是因为您订阅了contentSize ,请注意您发布的代码不会导致异常; it's because of what you are doing in the subscribe (or bind) that the problem is occurring.这是因为您在订阅(或绑定)中所做的事情正在发生。

As explained in the warning you received, an "Observable sequence is trying to send an event before sending of previous event has finished."正如您收到的警告中所解释的,“可观察序列正在尝试在前一个事件的发送完成之前发送一个事件。” This is usually caused by the programmer sending an onNext event on a Subject inside a subscribe that is tied to the subject.这通常是由程序员在绑定到主题的订阅中的主题上发送 onNext 事件引起的。 In this case, you might be changing the contentSize of the table view from inside the subscribe.在这种情况下,您可能会从订阅内部更改表视图的 contentSize。 The solution is to not do that.解决方案是不要那样做。

Explain more about why you think you need to do it and maybe I can show you a better way.解释更多关于为什么你认为你需要这样做,也许我可以告诉你一个更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM