I use Android Architecture Components to build my app. There is Paging Library to load items with Room generated DataSource
. Also there is BoundaryCallback
to get new data from server and store it in the database. It works fine, all is reactive, changes in the database come into PagedList.
But now I need to these items get some additional data, some calculations before they come into PagesList
and RecyclerView
. These calculations is not so fast to executing them on main thread in RecyclerView ViewHolder (actually I need to get additional data from the database or even from the server). So I supposed that I need to write my custom DataSource and make calculations there and then pass these processed items to PagedList
.
I created my ItemKeyedDataSource
(I'm not sure this is correct, because I load data from database, but this data source type is designed for network, but I don't think this is critical), and make queries in Dao that return List
of items. After I got a "page", I make calculations to items and then pass it to callback. It works, PagedList
gets processed items.
But unfortunately there is no reactivity with this approach. No changes in database come to my PagedList. I tried to return LiveData<List>
from Dao and add observeForever()
listener in DataSource
but it fails since you can't run it on background thread.
I watched Room generated DataSource.Factory
and LimitOffsetDataSource
but it doesn't look good to me since you need to pass table names to observe changes and other unclear things.
I suppose that I need to use invalidate(), but I don't because I have no idea where it should be.
There is 3 main questions:
- Is it right to process items in
DataSource
before they come toRecyclerView
or there is a better place? - Should I use
PositionalDataSource
instead ofItemKeyedDataSource
? - How can I add Room reactivity to custom
DataSource
?