user3354265
user3354265

Reputation: 825

Rxjava onNext not firing via retrofit observable

I have this simple retrofit2 api interface which contains

interface Api {
    @GET(BuildConfig.END_POINT) Observable<Response> fetchData();
}

So everything is fine when I'm doing a fresh request but let say I fire a request and I un-subscribe immediately and then I try to fire new request it returns nothing.

So, in code it looks something like this:

in Activity::onPause I perform un-subscription and in Activity::onResume I fire the request again.

My request looks something like this::

api.fetchData()
    .timeout(30,TimeUnit.SECONDS)
    .doOnNext(new Action1<Response>() {
        @Override public void call(Response response) {
            list = response.getDataForList();
        }
    }).flatMap(new Func1<Response, Observable<List<Object>>>() {
        @Override public Observable<Object>> call(Response response) {
            return Observable.just(list);
        }
    });

When I tried debugging it, the call is made but doOnNext() is not called. None of the lifecycle methods are called.

And just for clarification from here I'm just returning the observable which I'm using it somewhere else where I'm observing on main thread and subscribing on IO.

Upvotes: 3

Views: 1136

Answers (1)

paul
paul

Reputation: 13471

Don't use doOnNext, just map. And try to get use to use lambdas, make your code much readable.

api.fetchData()
.timeout(30,TimeUnit.SECONDS)
.map(response -> response.getDataForList())
.flatMap(list -> Observable.just(list));

Now as concept, every time that some observer subscribe to this observable consume the items, then observable automatically unsubscribe the observer. So you don't have to worry about unsubscribe anything.

You can see some practical examples here. https://github.com/politrons/reactive

Upvotes: 2

Related Questions