澄清:Observable 会在调用 onError() 后通知 onComplete() 吗?

Clearification: Would an Observable notify onComplete() after it calls onError()?

我需要在流结束时释放资源,无论是 onError 还是 onComplete。从通知中的 ReactiveX The Observable Contract 读取它说

An Observable may make zero or more OnNext notifications, each representing a single emitted item, and it may then follow those emission notifications by either an OnCompleted or an OnError notification, but not both.

我将清理调用同时放在 onError 和 onComplete 通知中是否正确?像这样:

.subscribe(
    //onNext
    completable -> Log.d(LOG_TAG,"done"),
    //onError
    throwable -> {
      Log.d(LOG_TAG,"error");
      serviceCleanup();
    },
    //onComplete
    this::serviceCleanup
);

例如。只是为了将来的文档目的,所以我可以向其他人和我自己解释。

您最好使用 doFinally or doAfterTerminate 来清理资源。