分页库-具有API的页面和大小的网络+数据库的边界回调 [英] Paging library - Boundary callback for network + db with API taking page and size
问题描述
简短问题:
使用API使用页面+大小加载新页面和BoundaryCallback
类的API,从Architecture组件处理分页库上的数据库+网络的正确方法是什么?
What is the correct way to handle database + network on the Paging library from Architecture components, using an API that uses page + size to load a new page and the BoundaryCallback
class?
研究与解释
当前在分页中使用的类 BoundaryCallback
用于体系结构组件的库,将列表中某个元素的实例作为参数接收,而没有该元素所在位置的实际上下文.它发生在onItemAtFrontLoaded
和onItemAtEndLoaded
中.
Currently the class BoundaryCallback
used in the paging library for architecture components, receives as parameter the instance of an element in the list without actual context of where this element is at. It happens in onItemAtFrontLoaded
and onItemAtEndLoaded
.
我的Api应该接收页面和页面大小以加载下一个数据块.作为分页列表构建器的一部分添加的边界回调应根据预取距离和页面大小告诉您何时加载下一页数据.
My Api is supposed to receive the page and the size of the page to load the next chunk of data. Boundary callback, added as part of the paged list builder, is supposed to tell you when to load the next page of data based on prefetch distance and page size.
由于Api需要页面号和页面大小才能提供,因此我没有看到一种仅通过接收onItemAtFrontLoaded
和onItemAtEndLoaded
.检查此链接中的google示例,它们使用的名称为获取下一个元素的最后一个元素,但这不适合页面+大小的Api.
Since the Api needs the page number and the size of the page to provide, I don't see a way to send that to the Api just by receiving one of the elements from the list as offered in onItemAtFrontLoaded
and onItemAtEndLoaded
. Checking the google examples in this link, they use the name of the last element to get the next one, but that doesn't fit an Api with page + size.
他们还有另一个示例,仅使用 PagedKeyedDatasource
,但是没有关于如何将其与数据库和BoundaryCallback混合使用的示例或线索.
They also have another example with only network that uses PagedKeyedDatasource
, but there is no sample or clue on how to mix that with the database and the BoundaryCallback.
修改: 到目前为止,我发现的唯一解决方案是将最后加载的页面存储在共享首选项上,但这听起来像是一个肮脏的把戏.
Edit: Only solutions I have found so far is to store the last loaded page on the shared preferences, but that sounds like a dirty trick.
请参阅 https://github.com/googlesamples/android-architecture-components/issues/252#issuecomment-392119468 进行官方输入.
Refer to https://github.com/googlesamples/android-architecture-components/issues/252#issuecomment-392119468 for official input on it.
推荐答案
我有一个类似的API(pageNum + size),我的data class
,pageNum
和pageSize
中有2个额外的字段,默认值为PAGE_SIZE
.
I have a similar API (pageNum + size), I have 2 extra fields in my data class
, pageNum
and pageSize
with defaults 1
and PAGE_SIZE
respectively.
如果您使用的是Network+DB
那么您将拥有onZeroItemsLoaded
和onItemAtEndLoaded
,
在onZeroItemsLoaded
中直接发送pageNum
和pageSize
,并在onItemAtEndLoaded
中将pageSize增大1
,然后发送.
If you're using Network+DB
then you'll have onZeroItemsLoaded
and onItemAtEndLoaded
,
In onZeroItemsLoaded
send pageNum
and pageSize
as it is, and in onItemAtEndLoaded
increment pageSize by 1
and then send.
假设您收到方法fetchData(pageNum, pageSize)
时,只需在此页面的每个项目中相应地更新pageNum
和pageSize
即可.
Let's say you have a method fetchData(pageNum, pageSize)
when you receive result in this just update the pageNum
and pageSize
accordingly in each item of this page.
这篇关于分页库-具有API的页面和大小的网络+数据库的边界回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!