How to use paging library with Room Dao without using any View classes

流过昼夜 提交于 2020-01-06 03:57:09

问题


I have an android service (no UI) with a class FooManager that needs to fetch all Foo objects (rows) in a database table foo using room and the androidx paging library.

All examples I have found show how to do this using the PagedListAdapter and RecyclerView. Since I have no UI I cannot use view classes.

Here is what I have tried:

  1. In my FooDao class I have the following method:
@Query("SELECT * FROM foo")
DataSource.Factory<Integer, Foo> getAllPaged();
  1. In the constructor for my FooManager class I initialize the Datasource as follows:
DataSource.Factory<Integer, Foo> fooDataSourceFactory = fooDao.getAllPaged();
PagedList.Config pagingConfig = new PagedList.Config.Builder()
        .setPageSize(PAGE_SIZE)
        .setPrefetchDistance(3 * PAGE_SIZE)
        .setEnablePlaceholders(false)
        .build();
LivePagedListBuilder<Integer, Foo> pagedListBuilder = new LivePagedListBuilder<>(fooDataSourceFactory, pagingConfig);

LiveData<PagedList<Foo>> pagedListLiveData = pagedListBuilder.build();
pagedListLiveData.observeForever(new Observer<PagedList<Foo>>() {
    @Override
    public void onChanged(PagedList<Foo> pagedList) {
        for (int i = 0; i < pagedList.size(); i++) {
            Foo foo = pageList.get(i);
            // Do some processing on foo in a background thread
        }
    }
});

This seems to work and fetches the first page of Foo objects that I can then iterate on and process. What I cannot figure out is how to get the next page of Foo objects and so on without using PagedListAdapter and RecyclerView.

It seems that I could use an AsyncPagedListDiffer but its not obvious how to use it.

Any suggestions?

来源:https://stackoverflow.com/questions/57794465/how-to-use-paging-library-with-room-dao-without-using-any-view-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!