I'm beginner in RXSwift, and i have problem with my code
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.
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?
I'm using 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. This causes a memory cycle as well.
As @Enigmativity suggested in the comments, you should use takeWhile()
to handle this situation:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With