简体   繁体   English

Android分页库-具有多种排序类型的数据+网络

[英]Android Paging Library - Data + Network with multiple sorting types

I recently saw that google has a great library for paginating data. 我最近看到google有一个很棒的用于分页数据的库。

In this Google IO: 在此Google IO中:

Android Jetpack: manage infinite lists with RecyclerView and Paging (Google I/O '18) Android Jetpack:使用RecyclerView和分页管理无限列表(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 . 他们解释了如何制作“ Data + Network DataSource并从数据库中获取数据作为Single Source of Truth并且当数据库中的数据不足时,它将使用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 ... 客户端(Android用户)可以按受欢迎程度,标题,date_release和...对它们进行排序。

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. 由于数据库中有大量电影(例如50部电影)可用,因此它将按受欢迎程度对少量电影进行排序,然后尝试从服务器获取数据,因此这不是很好的体验。

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. IMO不必为每种排序类型创建表,但是可以跟踪数据来自哪个端点。 For example, you have three endpoints with query ?sort=default , ?sort=ratings , ?sort=released_date . 例如,您有三个端​​点,查询为?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. 您可以在数据库模型类中添加三个额外的布尔属性(例如fromDefaultfromRatingsfromReleasedDate ),以跟踪其来自哪个端点,并在同一电影数据从多个端点到达时更新这些对应的标志

Now when you sort by rating, you will use SQL query to filter those where the flag fromRatings is true . 现在,当您按评分排序时,将使用SQL查询来过滤那些标记fromRatingstrue 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 . 但是最初,您将fromRatings ,因此您的BoundaryCallback会启动服务器请求,获取已排序的分级电影数据,并且在保存之前,您必须将fromRatings覆盖为true Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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