为什么分页库调用了几次就停止加载数据了?

Why does the paging library stop loading data after several calls?

我实现了一个分页库,但它不能正常工作。我从 github 请求数据并对存储库列表进行分页。该代码运行良好,但在对搜索查询进行多次更改后,它停止加载数据。在调试中,数据始终加载良好。我想问题出在异步上,但我不知道去哪里找。 我的代码:

RepoDataSource

public class RepoDataSource extends PageKeyedDataSource<Integer, Repo> {
@Override
    public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, Repo> callback) {
        Timber.d("Initial RepoDataSource");
        try {
            Response<RepoSearchResponse> response = githubService.searchRepos(query, firstNumberPage).execute();
            RepoSearchResponse repoSearchResponse = response.body();
            if (repoSearchResponse != null) {
                List<Repo> items = repoSearchResponse.getItems();
                callback.onResult(items, 1, 2);
            }
        } catch (IOException e) {
            Timber.i(e);
        }
    }

    @Override
    public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, Repo> callback) {
    }

    @Override
    public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, Repo> callback) {
        Timber.d("Fetching next page: %s", params.key);
        try {
            Response<RepoSearchResponse> response = githubService.searchRepos(query, params.key).execute();
            if (response.isSuccessful()) {
                RepoSearchResponse repoSearchResponse = response.body();
                if (repoSearchResponse != null) {
                    List<Repo> items = repoSearchResponse.getItems();
                    callback.onResult(items, params.key + 1);
                }
            }
        } catch (IOException e) {
            Timber.i(e);
        }
    }
}

GithubApiCall

@GET("search/repositories")
    Call<RepoSearchResponse> searchRepos(@Query("q") String query, @Query("page") Integer page);

RepoDataSourceFactory

public class RepoDataSourceFactory extends DataSource.Factory<Integer, Repo> {

    private GithubService githubService;
    private String query;

    public RepoDataSourceFactory(GithubService githubService, String query) {
        this.githubService = githubService;
        this.query = query;
    }

    @NonNull
    @Override
    public DataSource<Integer, Repo> create() {
        return new RepoDataSource(githubService, query);
    }
}

存储库方法

public class RepoRepository {
...
...
public RepoDataSourceFactory getRepoPagedFactory(String query) {
        return new RepoDataSourceFactory(githubService, query);
    }
}

视图模型

public final class MyViewModel {
...
public MutableLiveData<String> searchQuery = new MutableLiveData<>();
...
public LiveData<PagedList<Repo>> getRepos() {
        return Transformations.switchMap(searchQuery, query -> {
            RepoDataSourceFactory factory = repository.getRepoPagedFactory(query);
            return new LivePagedListBuilder<>(factory, pagedListConfig).build();
        });
    }
...
public SearchView.OnQueryTextListener listener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            if (query != null && !query.trim().equals("")) {
                searchQuery.postValue(query);
            }
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return true;
        }
    };
...
}

在我的 activity

viewModel.getRepos().observe(this, adapter::submitList);

您的代码没有任何问题。我在一个 GitHub 项目中,一直陷入同样的​​问题,直到我意识到 GitHub 对于未经身份验证的请求有一个 每分钟 10 个请求的速率限制 。但是如果是经过认证的,你每分钟最多可以发起30次请求。

我假设您也像我一样针对搜索查询中的每个更改发送请求,其中 typing/changing 5 个字符等于 5 个请求。所以真正的原因是来自 GitHub 的请求率非常有限,而不是你的代码。

看看这个:https://developer.github.com/v3/search/#rate-limit