Android Paging库sqlite数据存储问题 [英] Android Paging library sqlite data storage concern

本文介绍了Android Paging库sqlite数据存储问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天里,我对 Android体系结构组件感到很舒服,一直对分页库感兴趣,该分页用于处理和显示数据列表从远程源. 为了获得最佳的UX,最好先将数据加载到数据库中,在这种情况下, Room ,然后使用pageingLibrary和适配器在recyclerview中显示它.我担心的是,假设一个api甚至有1000条记录或一个Twitter提要,所有这些数据必须先通过db传递到UI,然后才能在设备上处理此存储?将所有数据存储在设备上是一个坏主意.有什么方法可以删除过时"数据,或者建议采用哪种方法?

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. For the best UX it is better to load data first into the database, in this case Room then display it in a recyclerview using the pagingLibrary and adapters. 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? 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?

android团队一直在为分页库开发V3,这里是一篇文章的链接,该文章可能会更清楚地显示

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

推荐答案

RemoteMediator具有initialize API,可以保证在加载开始之前完成该API.您可以重写initialize来检查数据是否陈旧,以执行所需的任何设置(例如修剪旧条目),但是如果仅1000行引起严重的性能问题,我个人会感到有些惊讶.

RemoteMediator has an initialize API which is guaranteed to complete before loading starts. 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.

对于诸如此类PagingSource查询之类的热代码路径,您可以查看androidx.benchmark以确定是否值得对其进行优化.

For hot code paths such as these PagingSource queries, you can look to androidx.benchmark to figure out if it's worth optimizing for.

如果您不想让任何旧的tweet保持刷新状态,那您可能只想考虑在成功的远程刷新后清除表,因为这是最简单的方法. initialize用于需要手动修剪/设置/陈旧性检查的情况(例如,您可以检查数据的年代,并确定是否要刷新),但只需在插入之前在远程刷新中清除即可新页面效果很好,因为无论如何您都希望重新启动分页.确保在同一事务中执行清除+插入操作,以免由于数据库更新而导致两个无效!

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 (e.g., 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. Make sure to do the clear + insert in the same transaction so you don't end up with two invalidates due to db updates!

这篇关于Android Paging库sqlite数据存储问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆