简体   繁体   English

RXSwift,检测到折返异常

[英]RXSwift, Reentrancy anomaly was detected

I'm beginner in RXSwift, and i have problem with my code 我是RXSwift的初学者,我的代码有问题

I have code: 我有代码:

let dartScore = PublishSubject<Int>()
            dartScore.asObservable()
                .scan(501) { intermediate, newValue in
                    let result = intermediate - newValue
                    return result >= 0 ? result : intermediate
                }
                .do(onNext: {
                    if $0 == 0 {
                        dartScore.onCompleted()
                    }
                })
                .subscribe({
                    print($0.isStopEvent ? $0 : $0.element!)
                })
                .disposed(by: disposeBag)

            dartScore.onNext(13)
            dartScore.onNext(50)
            dartScore.onNext(60)
            dartScore.onNext(378)

And i get error: 我得到错误:

⚠️ Reentrancy anomaly was detected. ⚠️重新进入异常被检测到。 ⚠️ ⚠️

Debugging: To debug this issue you can set a breakpoint in /****RxSwift/RxSwift/Rx.swift:97 and observe the call stack. 调试:要调试此问题,可以在/****RxSwift/RxSwift/Rx.swift:97中设置断点,并观察调用堆栈。

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. 可观察到的序列正在尝试发送事件,而先前事件的发送尚未完成。

why i can't do ".onCompleted()" inside .do(onNext), and what should i do to avoid the warning? 为什么我不能在.do(onNext)内执行“ .onCompleted()”,我应该怎么做以避免警告?

I'm using XCode 9.0, swift 4, RXSwift 4.0.0 我正在使用XCode 9.0,Swift 4,RXSwift 4.0.0

Thank you 谢谢

Best Regards 最好的祝福

You can't do the .onCompleted() inside the .onNext() because you would have the observable eating its own tail in that case. 您无法在.onCompleted()内部执行.onNext()因为在这种情况下,您将拥有可观察到的吞噬自己的尾巴的功能。 This causes a memory cycle as well. 这也会导致一个存储周期。

As @Enigmativity suggested in the comments, you should use takeWhile() to handle this situation: 就像注释中建议的@Enigmativity一样,您应该使用takeWhile()处理这种情况:

dartScore.asObservable()
    .scan(501) { intermediate, newValue in
        let result = intermediate - newValue
        return result >= 0 ? result : intermediate
    }
    .takeWhile { $0 != 0 }
    .subscribe({
        print($0.isStopEvent ? $0 : $0.element!)
    })

The above produces a new observable that completes when the value is 0. 上面的代码产生了一个新的observable,当该值为0时完成。

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

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