RXSwift,检测重入异常

我在RXSwift初学者,我有问题,我的代码RXSwift,检测重入异常

我有代码:

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)

,我得到错误:

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

为什么我不能在.do(onNext)中执行“.onCompleted()”,我该怎么做才能避免警告?

我使用的XCode 9.0,快捷4,RXSwift 4.0.0

谢谢

问候

回答:

你不能做.onCompleted().onNext()里面,因为你将有可观察在这种情况下吃自己的尾巴。这也会导致一个内存循环。

由于@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!)

})

上述方法产生一个新的观察到当值0

以上是 RXSwift,检测重入异常 的全部内容, 来源链接: utcz.com/qa/259393.html

回到顶部