4
votes

I need to release resources when a stream is finished, either onError or onComplete. Reading from the ReactiveX The Observable Contract on Notification it saids

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.

Am I correct to put the cleanup call in both onError and onComplete notification? like so:

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

eg. just for future documentation purposes so I can explain it to others, and myself.

1

1 Answers

3
votes

You should better use either doFinally or doAfterTerminate for resources cleaning up.