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.
subscribeOnandobserveOncan be combined usingcompose. - post_zeewisOnlinenot needed there. - Flippy