Reputation: 389
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
Upvotes: 10
Views: 13376
Reputation: 9346
Not directly connected to the OP's problem, but I came here when searching for Reentrancy anomaly was detected
, and for me the reason was simply that I removed an addSubview(viewInQuestion)
line in the code for an NSView on MacOs, while still using the viewInQuestion
in the constraints setup ... adding the viewInQuestion
as a subview solved the issue.
Upvotes: 0
Reputation: 33967
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.
Upvotes: 12