jpganz18
jpganz18

Reputation: 5878

How can I return a Maybe<Single> in JavaRX coming from an Observable that could be empty

Im trying to do something simple with JavaRx, even tho the methods exist, they dont work as I thought and Im a little stuck with it.

Basically, I have something equivalent to:

Observable<String> filteredNames = Single.just("Jack")
                .toObservable()
                .filter(it -> "John".equals(it));

A call to an API (that returns a Single type and later I make it an observable so I can filter it, based on that filter I could return a value or not.

The expected output its to have a Mayble

So, Ive done this:

filteredNames.singleElement();

But seems like there is an error, neither firstElement();

No converter found for return value of type: class io.reactivex.internal.operators.observable.ObservableSingleMaybe.

I tried also

Maybe.just(filteredNames);

But its not possible since filteredNames has an Observable and not a String... Any idea how to do this?

I use JavaRx 2

Upvotes: 0

Views: 872

Answers (1)

tmarwen
tmarwen

Reputation: 16394

You should not need to transform the Single to an Observable in order to filter it.

Single already supports the filter operator:

Maybe<String> m = Single.just("Jack")
    .filter(it -> "John".equals(it));

The error is a hint that you are returning the Observable as a result being serialized / or converted which I would assume being a API endpoint. If this shows to be the case, make sure to return the framework supported type, e.g. for a Spring Webflux endpoint, you would return a Mono:

return Mono.from(Single.just("Jack").filter(it -> "John".equals(it)).toFlowable());

Upvotes: 1

Related Questions