我一直在尝试对retryWhen
上的RxSwift
运算符进行测试,但遇到了Reentrancy Anomaly
问题,这是代码:
Observable<Int>.create { observer in
observer.onNext(1)
observer.onNext(2)
observer.onNext(3)
observer.onNext(4)
observer.onError(RequestError.dataError)
return Disposables.create()
}
.retryWhen { error in
return error.enumerated().flatMap { (index, error) -> Observable<Int> in
let maxRetry = 1
print("index: \(index)")
return index < maxRetry ? Observable.timer(1, scheduler: MainScheduler.instance) : Observable.error(RequestError.tooMany)
}
}
.subscribe(onNext: { value in
print("This: \(value)")
}, onError: { error in
print("ERRRRRRR: \(error)")
})
.disposed(by: disposeBag)
上面的代码给出:
This: 1
This: 2
This: 3
This: 4
index: 0
This: 1
This: 2
This: 3
This: 4
index: 1
⚠️ Reentrancy anomaly was detected.
> Debugging: To debug this issue you can set a breakpoint in /Users/tony.lin/Documents/Snippet/MaterialiseTest/Pods/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.
> 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 enqueing sequence events in some other way.
⚠️ Reentrancy anomaly was detected.
> Debugging: To debug this issue you can set a breakpoint in /Users/tony.lin/Documents/Snippet/MaterialiseTest/Pods/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.
> 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 enqueing sequence events in some other way.
ERRRRRRR: tooMany
只是想知道是否有人知道此问题的原因吗?
答案 0 :(得分:2)
如控制台注释所述,可以使用.observeOn(MainScheduler.asyncInstance)
来禁止显示此警告,如下所示:
Observable<Int>.from([1, 2, 3, 4]).concat(Observable.error(RequestError.dataError))
.observeOn(MainScheduler.asyncInstance) // this is the magic that makes it work.
.retryWhen { error in
return error.enumerated().flatMap { (index, error) -> Observable<Int> in
let maxRetry = 1
print("Index:", index)
guard index < maxRetry else { throw RequestError.tooMany }
return Observable.timer(1, scheduler: MainScheduler.instance)
}
}
.subscribe(onNext: { value in
print("This: \(value)")
}, onError: { error in
print("ERRRRRRR: \(error)")
})
我可以自由地对示例代码进行一些小的调整,以显示写出所拥有内容的另一种方法。
您要求解释(a)为什么添加ObserveOn起作用,以及(b)为什么需要它。
.observeOn(MainScheduler.asyncInstance)
的作用是将请求路由到一个备用线程,该事件可以在该线程上结束,然后在主线程上再次发出该事件。换句话说,就像这样做:
.observeOn(backgroundScheduler).observeOn(MainScheduler.instance)
backgroundScheduler
的定义如下:
let backgroundScheduler = SerialDispatchQueueScheduler(qos: .default)
至少这是我的理解。
关于为什么需要它,我不能说。您可能在库中发现了一个错误,因为使用1秒钟的延迟可以很好地运行而无需使用watchOn。