Rxjava: 2.x: Convert Observable to Single without a default value

Created on 23 Sep 2016  路  11Comments  路  Source: ReactiveX/RxJava

With the RC3 changes I don't see an easy way of converting an Observable into a Single without giving a default value.

How do you feel about adding methods for that?

Question

Most helpful comment

So basically add to Observable + Flowable:

singleOrError
firstOrError
lastOrError
elementAtOrError

All 11 comments

@abersnaze didn't like throwing because being empty. Try your API through Maybe and switchIfEmpty(Maybe.error()).

Edit wrong method name, fixed.

defaultIfEmpty(MaybeSource<T>) does not exist

Yeah sorry, switchIfEmpty() (I sometimes mix the two as defaultIfEmpty delegates to switchIfEmpty).

I don't quite see how switchIfEmpty solves my original problem. Maybe.switchIfEmpty() still returns a Maybe.

Also I'd rather have a NoSuchElementException and fail early than passing in a default value when converting an Observable to Single.

You could restore toSingle (under singleOrError), only the surface API has been removed.

So basically add to Observable + Flowable:

singleOrError
firstOrError
lastOrError
elementAtOrError

Yes.

Alright cool will do

Does singleElement() error on more than one but not on empty?

Yes, that was kept and also why firstElement is there to ignore more without signalling.

This can be closed right?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dzharikhin picture dzharikhin  路  4Comments

midnight-wonderer picture midnight-wonderer  路  3Comments

francorolando picture francorolando  路  3Comments

hoc081098 picture hoc081098  路  3Comments

dsvoronin picture dsvoronin  路  4Comments