Rxjava: No "isCompleted" or "isErrored" methods on Observable

Created on 4 Jun 2014  路  6Comments  路  Source: ReactiveX/RxJava

Hey folks,

I was currently running into the situation where I just needed a quick check if my observable is already done (and in particular failed), but I wondered why this is not exposed on the Observable - is there any specific reason?

my code was like

if (!observable.isCompleted()) {
 rescheduleIt();
}

Most helpful comment

If an Observer/Subscriber receives and onCompleted or onError (or doesn't want to any more onNext for any other reason) then it should unsubscribe from the Subscription. In a way isUnsubscribed on the Subscription/Subscriber can be used as a proxy for isComplete() || isError()

All 6 comments

If you need this, your code is probably too imperative.

But more importantly, it makes no sense for cold observables like Observable.range(1, 10) because that is never completed.

Assuming observable is _hot_, you can implement it yourself by doing

var isCompleted = false;
val s  = observable.subscribe(x => {}, (e)=>{},()=>{ isCompleted = true; })
...
if (!isCompleted) { rescheduleIt(); }

Note it is dangerous to rely on since there is a race condition

if (!isCompleted) {
  // now it can become completed right here, so you will reschedule anyhow.
 rescheduleIt();
}

@headinthebox thanks, my observable in this case is hot.
The approach here the request which contains the response observable can get redistributed and retried, but the ringbuffer could back it off with a backpressure exception, making it fail.

I'd like to now if it has failed before rescheduling it again.

How about using doOnCompleted?

@akarnokd I think I'll go with something simpler, I can fix that in a different place.

It's just something I need to wrap my head around in general I guess, but thanks for the clarification ;). closing then

If an Observer/Subscriber receives and onCompleted or onError (or doesn't want to any more onNext for any other reason) then it should unsubscribe from the Subscription. In a way isUnsubscribed on the Subscription/Subscriber can be used as a proxy for isComplete() || isError()

Nice catch @abersnaze !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

perlow picture perlow  路  3Comments

archenroot picture archenroot  路  3Comments

hoc081098 picture hoc081098  路  3Comments

paulblessing picture paulblessing  路  3Comments

dsvoronin picture dsvoronin  路  4Comments