private void apiPlace(){ if (isOnline(App.getContext())){ DiskCache.clearAll(); Observable.intervalRange(10, 5, 1, 2, TimeUnit.SECONDS) .map(integer -> integer * 10-90) .doOnSubscribe(disposable1 -> getViewState().showProgress()) .doFinally(() -> getViewState().hideProgress()) .subscribeOn(Schedulers.io()) .subscribe(integer -> { ApiRequest.getPlace(integer) .observeOn(AndroidSchedulers.mainThread()) .subscribe(place -> { getViewState().addMarkersOnMap(place); DiskCache.savePlace(place); }); },throwable -> { Log.e("MainPresenter", throwable.getMessage() + ""); }); }else { Observable.fromIterable(DiskCache.restorePlace()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .doOnSubscribe(disposable1 -> getViewState().showProgress()) .doFinally(() -> getViewState().hideProgress()) .subscribe(place -> { getViewState().addMarkersOnMap(place); },throwable -> { Log.e("MainPresenter", throwable.getMessage() + ""); }); } } 

RxJava began to learn recently. But basically I understood how everything works. But I look at this code and understand that it is terrible. A lot of what is repeated.

Can anyone optimize the code, it will help me very much to understand how to write correctly in the future. Yes, and people who start thinking will also have to deal with multithreading and code like this.

In short, a check is made for offline and online mode. If online, then we make 5 requests to the Internet and receive data and send it to View. If offline, then we pull out the data from the database and send it to the View.

  • one
    subscribeOn and observeOn can be combined using compose . - post_zeew
  • @post_zeew; Would it be difficult for you to post the code? - user239760
  • one
    well, Rx is not omnipotent and, in principle, your code meets the standards, still make compose as advised in the first comment, well, I understand you, scare the stairs in subscribe. You can add to the separate methods all that lies in the subscribe and in the subscibe itself call the method through the function :: this method link - Sviat Volkov
  • one
    I see the problem here not in Rx, but in the codeStyle itself, and simply non-observance of the principles of OOP. starting from the name of the methods, from the static context, the names of the classes, different brace logic, by the way, in Rx, you can also edit it - Shwarz Andrei
  • one
    Isn't it easier to make a query regardless of anything, to save the resulting collection in the database, then output everything from the database? I believe that in your case all isOnline not needed there. - Flippy

1 answer 1

 private void getPlaceFromApi(){ // if isOnline - проверка в отдельном месте discService.clearAll() // вместо DiskCache.clearAll() заимплементить интерфейс и дать в конструктор текущего класса; ApiRequest.getPlace(/*нужный инт*/) .subscribeOn(Schedulers.io()) // убрать в compose .observeOn(AndroidSchedulers.mainThread()) // убрать в compose .repeatWhen(/*описать условие, когда надо повторять*/) .doOnSubscribe(/*показали прогресс*/) .doOnComplete(/*спрятали прогресс*/) .subscribe(place -> /*и так далее*/ , throwable -> /*обработка ошибки, если много логики, то описать метод в классе типа handleError(Throwable t) и вызвать отсюда this::handleError*/); }