关于自定义 RxSwift Observable 扩展的问题

Question about a custom RxSwift Observable extension

extension Observable {
    func dispatchAsyncMainScheduler() -> Observable<E> {
        return self.observeOn(backgroundScheduler).observeOn(MainScheduler.instance)
    }
}

我在 https://github.com/artsy/eidolon

中找到了这个片段

我是这里的 reactive 初学者。据我了解。 subscribeOn 适用于整个链,而 observeOn 适用于其下方的运算符。

我的问题很明显,这两个连续的 observeOn 到底做了什么?

我觉得像是打字错误。应该是 .subscribeOn(backgroundScheduler)

这不是错字。如果您查看此运算符上方 10 行的 backgroundScheduler 定义,您会发现它是一个 SerialDispatchQueueSceduler。这意味着它将操作排队并在后台线程上按顺序执行它们。

他们用它来避免重入异常。我假设这段代码是在 MainScheduler.asyncInstance 被放入库之前编写的,它做同样的事情。

可以在 Rx 库中找到更多信息:

  • 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.