gpt4 book ai didi

android - 分页库 - 网络 + db 的边界回调,API 获取页面和大小

转载 作者:IT王子 更新时间:2023-10-28 23:39:29 24 4
gpt4 key购买 nike

小问题:

使用使用页面+大小加载新页面的API和BoundaryCallback类从架构组件处理分页库上的数据库+网络的正确方法是什么?

研究与解释

目前类(class)BoundaryCallback在架构组件的分页库中使用,接收列表中元素的实例作为参数,而没有该元素所在位置的实际上下文。它发生在 onItemAtFrontLoadedonItemAtEndLoaded 中。

我的 Api 应该接收页面和页面大小以加载下一个数据 block 。作为分页列表构建器的一部分添加的边界回调应该告诉您何时根据预取距离和页面大小加载下一页数据。

由于 Api 需要提供页码和页面大小,因此我看不到仅通过接收 onItemAtFrontLoaded< 中提供的列表中的一个元素来将其发送到 Api 的方法onItemAtEndLoaded。查看 this link 中的 google 示例,他们使用最后一个元素的名称来获取下一个元素,但这不适合具有页面 + 大小的 Api。

他们还有另一个例子,只有网络使用 PagedKeyedDatasource ,但没有关于如何将其与数据库和 BoundaryCallback 混合的示例或线索。

编辑:到目前为止,我发现的唯一解决方案是将最后加载的页面存储在共享首选项中,但这听起来像是一个肮脏的把戏。

引用 https://github.com/googlesamples/android-architecture-components/issues/252#issuecomment-392119468征求官方意见。

最佳答案

documentation在这个问题上有这样的说法:

If you aren't using an item-keyed network API, you may be usingpage-keyed, or page-indexed. If this is the case, the paging librarydoesn't know about the page key or index used in the BoundaryCallback,so you need to track it yourself. You can do this in one of two ways:

Local storage Page key

If you want to perfectly resume your query,even if the app is killed and resumed, you can store the key on disk.Note that with a positional/page index network API, there's a simpleway to do this, by using the listSize as an input to the next load (orlistSize / NETWORK_PAGE_SIZE, for page indexing). The current listsize isn't passed to the BoundaryCallback though. This is because thePagedList doesn't necessarily know the number of items in localstorage. Placeholders may be disabled, or the DataSource may not counttotal number of items.

Instead, for these positional cases, you can query the database forthe number of items, and pass that to the network.

In-Memory Page key

Often it doesn't make sense to query the next pagefrom network if the last page you fetched was loaded many hours ordays before. If you keep the key in memory, you can refresh any timeyou start paging from a network source. Store the next key in memory,inside your BoundaryCallback. When you create a new BoundaryCallbackwhen creating a new LiveData/Observable of PagedList, refresh data.For example, in the Paging Codelab, the GitHub network page index isstored in memory.

以及指向示例 Codelab 的链接:https://codelabs.developers.google.com/codelabs/android-paging/index.html#8

关于android - 分页库 - 网络 + db 的边界回调,API 获取页面和大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50456919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com