0

while learing rxjava/rxandroid i have came across one scenario. please help me to understand it. please check below code.

val observable:Observable<Int> = Observable.create {
            Log.d(TAG, "start emission")
            it.onNext(1)
            it.onNext(2)
            it.onNext(3)
            it.onError(RuntimeException())
            Log.d(TAG, "isObserverDispose: ${it.isDisposed}")
            it.onNext(4)
            Log.d(TAG, "end emission")
        }

        val v = observable.subscribe({
            Log.d(TAG, "onNext: $it")

        },{
            Log.d(TAG, "onError: $it")
        })

output

MainActivity: start emission

MainActivity: onNext: 1

MainActivity: onNext: 2

MainActivity: onNext: 3

MainActivity: onError: java.lang.RuntimeException

MainActivity: isObserverDispose: true

MainActivity: end emission

so as per my understanding, After calling onError(3) subscription need to end and if there is any emission after ending subscription, app will crash. But as per output app is not crashing. Can someone help me to understand what is going here.

akash
  • 127
  • 1
  • 8
  • 1
    "so as per my understanding" - Where in the `Emitter` interface contract and `ObservableEmitter` documentation does it state it should throw an error if the `Emitter` calls `onNext` after a terminal operator? The important thing is that the subscriber behaviour, after a terminal operator, does not observe the emission and clean-up has occurred. – Mark Dec 11 '21 at 13:52
  • Thanks mark , Now i understood why app is not crashing, because I am calling onNext(4) after onError(). App will crash if I call onError() after subscription ends. Basically I was trying understand here use case of RxJavaPlugins.errorHandler . – akash Dec 11 '21 at 13:59
  • I suggest you look at the source code of `ObservableCreate.CreateEmitter` - which is the concrete implementation https://github.com/ReactiveX/RxJava/blob/3.x/src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableCreate.java – Mark Dec 11 '21 at 14:00
  • "App will crash if I call onError() after subscription ends" - look at `tryOnError` also thats why you should implement `RxJavaPlugins.setErrorHandler( ... error handler consumer ... )` – Mark Dec 11 '21 at 14:11

0 Answers0