Observable 应该在 Subscriber 处理时调用 .onComplete() 吗?

Should Observable call .onComplete() when Subscriber disposes?

我正在编写一个 Observable 来处理与 Android 服务的连接,以 RxAndroidBle.establishConnection() 为模型。

我知道 .establishConnection() 从不调用 .onComplete();在发出连接后,它要么由订阅者处理,要么以错误结束(通常是在连接丢失的情况下)。但是在处理连接时调用 .onComplete() 似乎是合理的。我还没有找到关于此的官方 RxJava 政策;似乎有些 Observable 会这样做,而有些则不会。发出单个项目然后被处置的 Observable 的正确行为是什么?

What is the correct behavior for an Observable that emits a single item and is then disposed?

如果我理解正确的话,这是一个关于 Observable 在根据官方反应策略处理时应该如何表现的问题。实际上, calling/not 在处理时调用 .onComplete()Observable's contract 对齐,但后者似乎是首选(加粗我的):

Observable Termination

An Observable may begin issuing notifications to an observer immediately after the Observable receives a Subscribe notification from the observer.

When an observer issues an Unsubscribe notification to an Observable, the Observable will attempt to stop issuing notifications to the observer. It is not guaranteed, however, that the Observable will issue no notifications to the observer after an observer issues it an Unsubscribe notification.

When an Observable issues an OnError or OnComplete notification to its observers, this ends the subscription. Observers do not need to issue an Unsubscribe notification to end subscriptions that are ended by the Observable in this way.

通知在哪里:

Observer

  • OnNext
  • OnCompleted
  • OnError
  • OnSubscribe(可选)

ObservableSource

  • Subscribe
  • Unsubscribe
  • Request(可选)