我对 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/