1

The following Subscription doesn't work at all, When Click a refresh Button. There is no printed Logs indicate that is there any API called.

-Notes: All Dependencies done using dagger2, and Retrofit instance created like this:

@AppScope
    @Provides
    public Retrofit retrofit(OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl("https://~~~~~~~~~~")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }
  • Dagger compiled successfully.

[-]In the Presenter..

private final CompositeSubscription compositeSubscription = new CompositeSubscription();
...
//inside onCreate{}
compositeSubscription.add(observeRefresh());
//inside onDestroy{}
compositeSubscription.clear();

// *Here is the part I think that the problem in.

private Subscription observeRefresh() {
        return view.observeBtnRefresh()
                .doOnNext(__ -> view.showLoading())
                .observeOn(Schedulers.io())
                .switchMap(__ -> model.getFoodsResponse())// [-] In model below...
                .observeOn(AndroidSchedulers.mainThread())
//                .doOnNext(model::saveFoodsState)
                .doOnEach(__ -> view.hideLoading())
                .retry()
                .subscribe();
    }

[-] In the view ...

@BindView(R.id.btnRefresh)
    ImageButton btnRefresh;// ButterKnife.bind(this); is Done.
..
public Observable<Void> observeBtnRefresh() {
        return RxView.clicks(btnRefresh);
    }

[-] In the model

@Inject
    FoodsNetwork foodsNetwork;

    public Observable<Response<ResponseBody>> getFoodsResponse() {
        return foodsNetwork.getFoodsResponse();
    }

[-] foodsNetwork ...

public interface FoodsNetwork {

    @GET()
    Observable<Response<ResponseBody>> getFoodsResponse();

}

Thanks for reading any way!

Hussein Ahmed
  • 730
  • 2
  • 7
  • 17

2 Answers2

1

Change .observeOn(Schedulers.io()) to .subscribeOn(Schedulers.io()).

You want to do the work in a background thread, and observe the result in the main thread. See here for a tutorial.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • Thanks for your answer, I already tried with this before, but doesn't work! The difference is that in case obserOn() Background thread will start with it. But in case subscribeOn() will start from the next downstream. – Hussein Ahmed Sep 22 '18 at 17:43
0

I found what's wrong, and I decided to keep this question because could make other developers to take care of little things. There was a small thing missed in the API method(path and some @Query) and this was make it not working

@GET(".")
Observable<Response<ResponseBody>> getFoodsResponse(@Query String query);
Hussein Ahmed
  • 730
  • 2
  • 7
  • 17