问题
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:
- In my
FooDao
class I have the following method:
@Query("SELECT * FROM foo")
DataSource.Factory<Integer, Foo> getAllPaged();
- 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