gpt4 book ai didi

ios - 观察多个controlEvent导致 `Reentrancy anomaly was detected.`警告信息

转载 作者:行者123 更新时间:2023-11-28 23:56:45 48 4
gpt4 key购买 nike

我是 RxSwift 的新手,今天我遇到了一个我无法解决的问题。我想观察 UITextField 中的 controlEvent

代码:

textField.rx
.controlEvent([.editingDidEndOnExit, .editingDidEnd])
.subscribe(onNext: { [weak self] in
// do stuff
})
.disposed(by: disposeBag)

我想现在 textField 辞职成为第一响应者或用户点击 return 按钮。当我执行一行时,代码工作正常:textField.resignFirstResponder() 但是当我点击 return 按钮时,我收到一条警告消息:

⚠️ Reentrancy anomaly was detected. Debugging: To debug this issue you can set a breakpoint in /Users/laxmorek/Documents/projects/meetingapplication-ios/Pods/RxSwift/RxSwift/Rx.swift:97 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 enqueing sequence events in some other way.

我不明白这个。

  • RxSwift 中观察多个 controlEvent 的正确方法是什么?
  • 为什么我会收到这条警告消息?我的观察设置有什么问题?

编辑

临时解决方法是我这样拆分我的代码:

textField.rx
.controlEvent(.editingDidEndOnExit)
.subscribe(onNext: { [weak self] in
self?.isSelected = false
})
.disposed(by: disposeBag)
textField.rx
.controlEvent(.editingDidEnd)
.subscribe(onNext: { [weak self] in
self?.isSelected = false
})
.disposed(by: disposeBag)

但是这个代码重复看起来不太好。 :/

最佳答案

这意味着事件是从不同的线程发送的。通过指定 MainScheduler.asyncInstance,您指定事件与 MainScheduler.asyncInstance 线程一起发送,这将避免 onNext 事件的并发问题。

试试这个:

textField.rx
.controlEvent([.editingDidEndOnExit, .editingDidEnd])
.observeOn(MainScheduler.asyncInstance)
.subscribe(onNext: { [weak self] in
// do stuff
})
.disposed(by: disposeBag)

关于ios - 观察多个controlEvent导致 `Reentrancy anomaly was detected.`警告信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51002262/

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