RxSwift:实现concatMap生成了重入异常检测到错误

问题描述 投票:0回答:2

我正在尝试在concatMap中实现RxSwift,但是当我尝试在concatMap中将可观察值设置为新值时,出现此错误:

Reentrancy anomaly was detected.
  > Debugging: To debug this issue you can set a breakpoint in /Users/SuperUser/repos/RxObserver/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.

这是我的代码:

    public let myNumbers = BehaviorRelay<String>(value: "")


    override func viewDidLoad() {
        super.viewDidLoad()
        processNumbers()
        myNumbers.accept("one")
        myNumbers.accept("two")
    }

    func processNumbers() {
        Observable.of(myNumbers).concatMap{ $0
        }.subscribe(onNext:{
            print($0)
            if $0 == "one" || $0 == "two"{
                self.myNumbers.accept("YEAH!")
            }
            }).disposed(by: disposeBag)

    }

你们都知道为什么我会出现这个错误?或如何更改我的实现以避免出现此错误/警告?

非常感谢您的帮助。

rx-swift xcode11.2 concatmap ios13.2 swift5.2
2个回答
0
投票

我找到了解决此问题的方法:

func processNumbers() {
        Observable.of(myNumbers).concatMap{ $0
        }
        .observeOn(MainScheduler.asyncInstance)
        .subscribe(onNext:{
            print($0)
            if $0 == "one" || $0 == "two"{
                self.myNumbers.accept("YEAH!")
            }
            }).disposed(by: disposeBag)

    }

通过添加这行代码.observeOn(MainScheduler.asyncInstance),错误/警告消失了!!


-1
投票

你们都知道为什么我会出现这个错误吗?

  > 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.

如何更改我的实现以避免出现此错误/警告?

  > 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.
© www.soinside.com 2019 - 2024. All rights reserved.