Ioa*_*dze 8 android android-room android-paging android-paging-library android-paging-3
我正在使用最新的 Jetpack 库。
Pagination3 版本:3.0.0-alpha05
房间版本:2.3.0-alpha02
我的实体有 Long asPrimaryKey和 Room 可以PagingSource为其他Int类型生成。
error: For now, Room only supports PagingSource with Key of type Int.
public abstract androidx.paging.PagingSource<java.lang.Long, com.example.myEntity>` getPagingSource();
Run Code Online (Sandbox Code Playgroud)
因此,我尝试实现我的 custom PagingSource,就像文档建议的那样。
问题是Data Refresh,因为 Room 生成的代码处理数据刷新,而我的代码无法处理这种情况。
任何建议如何实现自定义PagingSource的Room,也负责处理Data Refresh?
sho*_*ano -3
由于您有“刷新”场景并使用 Room 数据库,我猜测您正在使用具有网络+本地数据库模式的 Paging3(使用 Room 数据库作为本地缓存)。
我的网络+本地数据库模式也有类似的情况。我不确定我是否正确理解你的问题,或者你的情况与我的情况相同,但无论如何我都会分享我所做的。
我正在使用什么:
我所做的是让 Room 库创建 PagingSource (使用 的键Int),并让RemoteMediator处理所有其他情况,例如在刷新和/或追加时从网络获取数据,并在获取成功后立即将它们插入到数据库中。
我dao从 Room 库创建 PagingSource 的函数:
@Query("SELECT * FROM article WHERE isUnread = 1")
fun getUnreadPagingSource(): PagingSource<Int, LocalArticle>
Run Code Online (Sandbox Code Playgroud)
就我而言,我定义了 Repository 类,使其在其构造函数中包含类,以便在创建Pagerdao类时从存储库调用上述函数。
我的自定义 RemoteMediator 类如下所示:
RemoteMediator#load始终返回。trueloadTypeLoadType.PREPENDclass FeedMediator(
private val repository: FeedRepository
) : RemoteMediator<Int, LocalArticle>() {
...
override suspend fun load(
loadType: LoadType,
state: PagingState<Int, LocalArticle>
): MediatorResult = runCatching {
when (loadType) {
LoadType.PREPEND -> true
LoadType.REFRESH -> {
feedRepository.refresh()
false
}
LoadType.APPEND -> {
val continuation = feedRepository.continuation()
if (continuation.isNullOrEmpty()) {
true
} else {
loadFeedAndCheckContinuation(continuation)
}
}
}
}.fold(
onSuccess = { endOfPaginationReached -> MediatorResult.Success(endOfPaginationReached) },
onFailure = {
Timber.e(it)
MediatorResult.Error(it)
}
)
private suspend fun loadFeedAndCheckContinuation(continuation: String?): Boolean {
val feed = feedRepository.load(continuation)
feedRepository.insert(feed)
return feed.continuation.isNullOrEmpty()
}
Run Code Online (Sandbox Code Playgroud)
最后你可以创建Pager类。
fun createFeedPager(
mediator: FeedMediator<Int, LocalArticle>,
repository: FeedRepository
) = Pager(
config = PagingConfig(
pageSize = FETCH_FEED_COUNT,
enablePlaceholders = false,
prefetchDistance = PREFETCH_DISTANCE
),
remoteMediator = mediator,
pagingSourceFactory = { repository.getUnreadPagingSource() }
)
Run Code Online (Sandbox Code Playgroud)
我希望它能以某种方式有所帮助..
其他参考:
编辑:再次阅读文档后,我发现文档明确指出了一个声明:
RemoteMediator 用于将数据从网络加载到本地数据库中。
| 归档时间: |
|
| 查看次数: |
1689 次 |
| 最近记录: |