Android Paging 3.0
Adapting Android: Migrating from Paging 2.0 to Paging 3.0
As part of the Android 11 beta release, Google has also updated the Jetpack Paging library to 3.0, which according to the blog post, is a major update.
..a major update to Paging 3, rewritten Kotlin-first with full support for coroutines!
What’s Changed?
- A complete rewrite from 2.0 using Kotlin with coroutines.
- Built-in separator, header and footer support.
- Built-in retry and refresh mechanism.
Architecture
Despite being a complete rewrite, the architecture remains the same as 2.0.
As comparison, I drafted the common MVVM design pattern that’s been in use for a long while.
To map paging 2.0 against Figure 2, we get the following:
- Source ->
DataSource
- Pager ->
LivePagedListBuilder
- Data Stream ->
LiveData<PagedList<T>>
- Adapter ->
PagedListAdapter
Migration
With the same architecture, migration is almost a drop-in replacement of a few classes. Let’s visualize the migration as below.
Not all classes are required to changed, here’s to summarize:
# Not changed (or required)
- DiffUtil.ItemCallback can be reused as it is.
- DataSource.Factory is no longer required.
# Changed
Adapter
,Config
andPager
are all so similar that its almost a straight 1-to-1 mapping.ItemKeyedDataSource
,PageKeyedDataSource
andPositionalDataSource
are all merged into a singlePagingSource
.
Note: PagingSource
is backward compatible with LivePagedListBuilder
and RxPagedListBuilder
if you are using any of this.
Conclusion
- Migration from paging 2.0 to 3.0 seems easy enough.
- Migrating
DataSource
toPagingSource
might required more efforts. - Not sure if the added features (header, footer, etc) is going to be helpful at this point.
Here is a demo project that implements both paging 2.0 and paging 3.0. You can find a lot of similarities in between the 2 implementations.