3

I'm working on my custom Observables in an effort to make them more reusable throughout my code and wondering on the best [correct] way to do it.

What I am doing is just implementing the Observable.OnSubscribe<> interface and working at the call() method.

Basically, I want to subscribe to another Observable and depending on its result, emit one or other data conditionally. Is it a bad practice to subscribe to another Observable inside the aforementioned call() method? It seems clumsy to me at least. Or should I use an Rx operator in a different way that I'm (still) not aware of?

Note: I'm not using Java8 and Retrolambda, so please, don't use lambdas on any possible answers.

1 Answer 1

3

Creating custom Observables ranges from tricky to difficult and I wouldn't recommend it as the first step. Your scenario can be solved via flatMap:

source.flatMap(new Func1<Integer, Observable<Integer>>() {
    @Override
    public Observable<Integer> call(Integer value) {
        if (value < 5) {
            return Observable.empty();
        }
        return Observable.just(value * 2);
    }
});

Is it a bad practice to subscribe to another Observable inside the aforementioned call() method?

No, but you have to avoid what's called multiple onStart calls by wrapping the incoming Subscriber via Subscribers.wrap().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.