1

I started to learn about the paging library, And I've a problem.

I'm able to fetch the data and show inside my recyclerView, But I've really weird beahviors.

I Put logs on loadInitial, loadBefore and loadAfter and the first time loadInitial and loadAfter call one after another immediatly.

When I scroll down, I log getPage from the response and it give me the right page number after 20 item, but I really suspect it just load ALL the pages for the first time, I mean, I can literly scroll 500 item without wait to load even one time.

The first problem as I said - it called loadInitial and loadAfter one after another immediatly at the first time.

second problem - when I scroll up, loadBefore NEVER triggered.

I don't sure which code I should share, but I suspect the problem is somewhere inside the data source, If you need more let me know in the comments

CODE:

public class MoviesDataSource extends PageKeyedDataSource<Integer, Results> {

    private static final int FIRST_PAGE = 1;


    @Override
    public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, Results> callback) {
        Log.i(TAG, "loadInitial: ");

        ApiService.getAllMovies().getAllMovies(API_KEY, FIRST_PAGE).enqueue(
                new Callback<AllMovies>() {
                    @Override
                    public void onResponse(Call<AllMovies> call, Response<AllMovies> response) {
                        if (response.body() != null) {
                            Log.i(TAG, "onResponse: " + response.body().getPage());
                            List<Results> results = Arrays.asList(response.body().getResults());
                            callback.onResult(results, null, FIRST_PAGE + 1 );
                        }

                    }

                    @Override
                    public void onFailure(Call<AllMovies> call, Throwable t) {

                    }
                }
        );
    }

    @Override
    public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, Results> callback) {
        Log.i(TAG, "loadBefore: ");
        ApiService.getAllMovies().getAllMovies(API_KEY, params.key).enqueue(
                new Callback<AllMovies>() {
                    @Override
                    public void onResponse(Call<AllMovies> call, Response<AllMovies> response) {
                        Log.i(TAG, "onResponse: " + response.body().getPage());
                        Integer key = (params.key > 1) ? params.key -1 : null;

                        if (response.body() != null) {
                            List<Results> results = Arrays.asList(response.body().getResults());
                            callback.onResult(results, key );
                        }
                    }

                    @Override
                    public void onFailure(Call<AllMovies> call, Throwable t) {

                    }
                }
        );
    }

    @Override
    public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, Results> callback) {
        Log.i(TAG, "loadAfter: ");
        ApiService.getAllMovies().getAllMovies(API_KEY, params.key).enqueue(
                new Callback<AllMovies>() {
                    @Override
                    public void onResponse(Call<AllMovies> call, Response<AllMovies> response) {
                        Log.i(TAG, "onResponse: " + response.body().getPage());

                        Integer key = params.key + 1; // calculate until there is no data

                        if (response.body() != null) {
                            List<Results> results = Arrays.asList(response.body().getResults());
                            callback.onResult(results, key );
                        }
                    }

                    @Override
                    public void onFailure(Call<AllMovies> call, Throwable t) {

                    }
                }
        );
    }
}

MainActivity

    @Override
        public void onChanged(PagedList<Results> results) {
            adapter.submitList(results);
        }

ViewModel

ScrapeW
  • 489
  • 8
  • 16

1 Answers1

1

First problem: loadAfter gets called based on PagedList.Config prefetch distance and page size configuration.To control how and when a PagedList queries data from its DataSource, see PagedList.Config

second: you load data in one direction so loadBefore never gets called.

Also, you should be using synchronous retrofit calls, you can find reference here: network only paging Im developing app based on same api while learning about jetpack stuff, am little ahead of you (caching db + network) :)

Nikola Srdoč
  • 311
  • 4
  • 14