目前,我正在尝试迁移到Android的新分页3库,但如果我看对了,我无法:(
我使用AWS Amplify作为后端数据源,并希望在分页库的PaginSource类的新加载函数中包含一个查询。
override suspend fun load(params: LoadParams<String>): LoadResult<String, Car> {
val query = ListCarsQuery.builder().limit(params.loadSize).build()
appSyncClient.query(query)
.responseFetcher(AppSyncResponseFetchers.CACHE_AND_NETWORK)
.enqueue(
object : GraphQLCall.Callback<ListCarsQuery.Data>() {
override fun onResponse(response: Response<ListCarsQuery.Data>) {
val result = CarTransformer.toModels(response)
// Here is my actual result list
}
override fun onFailure(e: ApolloException) {
TODO("Not yet implemented")
}
}
)
//How can I add my result list here ?
return LoadResult.Page(
data = listOf(),
prevKey = null,
nextKey = ""
)由于方法入队给了我一个空的回调,我不知道如何等待它或触发回调,就像在分页库2中一样。在分页2中,我可以选择在enqueue().onResponse函数中调用callback.onResult(result.data,result.nextLink)方法,而不必返回任何东西。
有没有办法做到这一点,或者我应该坚持使用paging 2?
发布于 2020-08-14 16:01:50
Paging3目前还没有提供回调ListenableFuture,因此您需要将其封装到一个RxJava Single、一个Guava API或一个挂起的Kotlin协程中。
paging-rxjava2/3工件中提供了PagingSource的Rx版本,而paging-guava中提供了Guava的版本。
就实际的转换而言,列出所有的可能性可能会很多,但例如,有一些Kotlin Coroutine构建器允许您在挂起的上下文中包装和等待xallback。以suspendCancellableCoroutine为例,您基本上得到了一个可以调用resume(result)的Continuation对象。
https://stackoverflow.com/questions/63381868
复制相似问题