简体   繁体   English

Android 分页库 sqlite 数据存储关注

[英]Android Paging library sqlite data storage concern

Over the past days I have been getting comfortable with Android Architecture Components and I've been mostly interested in the Paging Library for handling and displaying lists of data from a remote source.在过去的几天里,我一直对Android 架构组件感到满意,并且我对用于处理和显示来自远程源的数据列表的分页库最感兴趣。 For the best UX it is better to load data first into the database, in this caseRoom then display it in a recyclerview using the pagingLibrary and adapters.为了获得最佳的用户体验,最好先将数据加载到数据库中,在这种情况下,Room然后使用 pagingLibrary 和适配器将其显示在 recyclerview 中。 My concern is this, lets say an api has 1000s of records or a twitter feed even and all this data has to pass through the db then to the UI, how to handle this storage on the device?我担心的是,假设 api 有 1000 条记录或 twitter 提要,所有这些数据都必须通过数据库然后传递到 UI,如何处理设备上的存储? Seeing as storing all the data on the device is a bad idea.将所有数据存储在设备上是一个坏主意。 Is there a way to remove 'stale' data or what is the recommended way of doing this?有没有办法删除“陈旧”数据或推荐的方法是什么?

Edit: The android team has been working on V3 for the paging library here is a link to an article that might shed more light article here编辑:android 团队一直在为分页库开发 V3 这里是一篇文章的链接,该文章可能会在此处提供更多信息

RemoteMediator has an initialize API which is guaranteed to complete before loading starts. RemoteMediator有一个initialize API 保证在加载开始之前完成。 You can override initialize to check for data staleness to do any setup you need (such as pruning old entries), though I'd personally be a bit surprised if only 1000 rows was causing significant performance issues.您可以覆盖initialize以检查数据陈旧性以执行您需要的任何设置(例如修剪旧条目),但如果只有 1000 行会导致严重的性能问题,我个人会感到有点惊讶。

For hot code paths such as these PagingSource queries, you can look to androidx.benchmark to figure out if it's worth optimizing for.对于诸如这些PagingSource查询之类的热代码路径,您可以查看 androidx.benchmark 以确定它是否值得优化。

Edit: You may want to consider simply clearing the table on a successful remote refresh if you don't want to keep any of the old tweets on refresh as that's the simplest approach.编辑:如果您不想在刷新时保留任何旧推文,您可能需要考虑在成功的远程刷新时简单地清除表格,因为这是最简单的方法。 initialize is meant for the case where you need to do manual pruning / setup / staleness checks (eg, you can check how old your data is and decide if you want to refresh or not), but simply clearing in remote refresh before inserting the new page works well because you'd expect to have to restart pagination anyway. initialize适用于您需要进行手动修剪/设置/过时检查的情况(例如,您可以检查数据的年龄并决定是否要刷新),但只需在插入新数据之前清除远程刷新page 运行良好,因为无论如何您都希望重新启动分页。 Make sure to do the clear + insert in the same transaction so you don't end up with two invalidates due to db updates!确保在同一个事务中执行 clear + insert 操作,这样您就不会因数据库更新而导致两次无效!

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

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