How to get data from DataSource.Factory

2019-07-24 15:36发布

问题:

I have to call this method to get all the persons. I cannot modify this method at all.

@Query("SELECT * FROM PERSON_TABLE ORDER BY NAME DESC"
abstract fun getElements(): DataSource.Factory<Int, Person>

Then in an Activity I am calling it like this:

override fun onCreate(...)
{
    ...

    val data = dao.getElements()
}

I want to get all the Persons, possibly as a list. How do I do this?

I don't understand how DataSource.Factory<Int, Person> works.

回答1:

According to the docs:

Typically, your UI code observes a LiveData object (or, if you're using RxJava2, a Flowable or Observable object), which resides in your app's ViewModel. This observable object forms a connection between the presentation and contents of your app's list data.

In order to create one of these observable PagedList objects, pass in an instance of DataSource.Factory to a LivePagedListBuilder or RxPagedListBuilder object. A DataSource object loads pages for a single PagedList. The factory class creates new instances of PagedList in response to content updates, such as database table invalidations and network refreshes. The Room persistence library can provide DataSource.Factory objects for you, or you can build your own.

The sample code is as following:

// The Int type argument corresponds to a PositionalDataSource object.
val data: DataSource.Factory<Int, Person> = dao.getElements()

val dataList: LiveData<PagedList<Person>> = LivePagedListBuilder(data, /* page size */ 20).build()

So you need to pass your DataSource.Factory<Int, Person> object to LivePagedListBuilder and at the end you will get LiveData<PagedList<Person>> which you can observe.


After that you need to connect LiveData to a PagedListAdapter as shown in the following code snippet:

private val adapter = YourDataAdapter()

override fun onCreate(savedInstanceState: Bundle?) {
    dataList.observe(this, Observer { adapter.submitList(it) })
}

The sample code of adapter you can find here.



回答2:

Adding to @Sergey 's answer, You can use PagedList<>.snapshot() to get as a regular list

 // Get the DataSource from the database
 val dataSource: DataSource.Factory<Int, Person> = dao.getElements()

 // Get the dataSource as LiveData
 val data = dataSource.toLiveData(20 /* page size */)

 // In UI
 vm.data.observer(this, Observer{pagedList->
  // To get the pagedList as a regular list -
  val dataList = pagedList.snapshot()
 })

Note: The snapshot() function only gives you the currently available items in your pagedList.