简体   繁体   中英

Android Paging Library - Data + Network with multiple sorting types

I recently saw that google has a great library for paginating data.

In this Google IO:

Android Jetpack: manage infinite lists with RecyclerView and Paging (Google I/O '18)

They explained how to make Data + Network DataSource and grab data from database as Single Source of Truth and when the database is out of data it request for more data from the network with BoundaryCallback .

So assume that I have a list of Movies on the server. And client (Android user) can sort them by popularity, title, date_release and ...

So in the first time if the user sort movies by titles everything will work great because there is no data in database and data will be requested from the server to sort them by title and send them back. But what if after that user tries to sort them by popularity for example? Because of the availability of chunk of movies in database (for example 50 movies), it will sort this small amount of movies by popularity and then tries to grab data from server and this is not a good experience.

I cant make table for every sort type because it is not a good practice. So how can I overcome to this problem?

Thanks very much

Your question makes sense. IMO you do not have to make tables for every sort types but you can keep a track of which endpoint the data came from. For example, you have three endpoints with query ?sort=default , ?sort=ratings , ?sort=released_date . You can add three extra Boolean attributes (say fromDefault , fromRatings , fromReleasedDate ) to your db model class to keep track of which endpoint it came from & update those corresponding flag whenever same movie data arrives from multiple endpoints.

Now when you sort by rating, you will use SQL query to filter those where the flag fromRatings is true . But initially, you will have none so your BoundaryCallback fires up the server request, gets the ratings sorted movie data and before saving, you'll have to override the fromRatings to true . Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM