How to stop loading in my method. I’m using retrofit+rxJava

  android, java, retrofit, rx-java

when I start the application, at first, the data is loaded as it should, but then the loader does not stop and constantly downloads data and the adapter is constantly updated, how to stop the loader so that it is called when needed. Check out my method. Here is my code. If there is a lot of information, I also attach a link to the github where the project itself is: https://github.com/johnzieman/watchex.git

methods in:
MainActivity.class

   adapter.setGetEndListener(new MovieAdapter.OnGetEndListener() {
        @Override
        public void getEndListener() {
            if (!isLoading) {
                Toast.makeText(MainActivity.this, "End", Toast.LENGTH_LONG).show();
                getData(numb);
            }
        }
    });
}


public void getData(int i) {
    viewModel.loadData(i, page);
    viewModel.setOnStartLoadingListener(new MovieViewModel.onStartLoadingListener() {
        @Override
        public void onStartLoading() {
            isLoading = true;
        }
    });
    viewModel.setOnFinishLoadingLisneter(new MovieViewModel.onFinishLoadingLisneter() {
        @Override
        public void onFinishLoading(List<Movie> movies) {
            if (movies != null && !movies.isEmpty()) {
                if (page == 1) {
                    adapter.clearAdapter();
                    viewModel.deleteAllMovies();
                }
                viewModel.insertAllMovies(movies);
                adapter.setMovies(movies);
                ++page;
            }
            isLoading = false;
        }
    });
}

also MovieViewModel.class method

public void loadData(int i, int page) {
    String sortType = null;
    if (i == 0) {
        sortType = POPULARITY;
    } else if (i == 1) {
        sortType = RATED;
    }
    if(onStartLoadingListener!=null){
        onStartLoadingListener.onStartLoading();
    }
    MovieDownloadFactory movieDownloadFactory = MovieDownloadFactory.getMovieDownloadFactory();
    MovieFactoryService movieFactoryService = movieDownloadFactory.getMovieFactoryService();

    disposable = movieFactoryService.getResultsResponse(API_KEY, LANGUAGE, sortType, VOTES, page)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<ResultsResponse>() {
                @Override
                public void accept(ResultsResponse resultsResponses) throws Exception {
                    if (resultsResponses != null) {
                        if(onFinishLoadingLisneter!=null){
                            onFinishLoadingLisneter.onFinishLoading(resultsResponses.getMovies());
                        }
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    Log.i("WrongMessage", Objects.requireNonNull(throwable.getMessage()));
                }
            });
    compositeDisposable.add(disposable);
}

Source: Android Questions

LEAVE A COMMENT