Android:Paging3 使用 cloud-firestore 创建远程中介

问题描述

我正在尝试使用 Paging3 创建一个 RemoteMediator,我在其中缓存我的本地结果以节省网络流量、云 Firestore 文档访问并计算每个查询的项目数量,以显示占位符。

我已经成功实现了“唯一的网络方式”,方法是实现一个分页源来访问我的 cloud-firestore 并加载下一页。但我正在努力使用 RemoteMediator 方式,因为有零个教程如何与 cloud-firestore 结合使用。

我将提供我当前的方法(pagingSource 方法)以及我使用 RemoteMediator 的进展。感谢您的帮助。

存储库

@Singleton
class ShopPagingRepositoryImpl @Inject constructor(private val db: FirebaseFirestore) : ShopPagingRepository {

    override suspend fun getCurrentPage(query: QueryHolder): QuerySnapshot = db.collection(FIREBASE_PRODUCTS_BASE_PATH)
        .limit(SHOP_DB_DOCUMENT_LIMIT)
        .whereEqualTo(FIREBASE_PRODUCT_CATEGORY,query.category)
        .orderBy(query.order)
        .get()
        .await()

    override suspend fun getNextPage(lastDocument: DocumentSnapshot,query: QueryHolder): QuerySnapshot = db.collection(FIREBASE_PRODUCTS_BASE_PATH)
        .limit(SHOP_DB_DOCUMENT_LIMIT)
        .orderBy(query.order,query.direction)
        .startAfter(lastDocument)
        .whereEqualTo(FIREBASE_PRODUCT_CATEGORY,query.category)
        .get()
        .await()
}

目前的方法(工作)

class ShopPagingSource(
    private val shopRepository: ShopPagingRepository,private val query: QueryHolder
) : PagingSource<QuerySnapshot,Product>() {

    private companion object {
        const val SHOP_MAX_LOADING_TIME: Long = 5000L
    }

    override suspend fun load(params: LoadParams<QuerySnapshot>): LoadResult<QuerySnapshot,Product> {
        return try {
            withTimeout(SHOP_MAX_LOADING_TIME) {
                val currentPage = params.key ?: shopRepository.getCurrentPage(query)

                val nextPage: QuerySnapshot? = if (currentPage.size() != 0) {
                    val lastDocumentSnapShot = currentPage.documents[currentPage.size() - 1]
                    shopRepository.getNextPage(lastDocumentSnapShot,query)
                } else null

                LoadResult.Page(
                    data = currentPage.toObjects(),prevKey = null,nextKey = nextPage
                )
            }
        } catch (e: Exception) {
            Timber.e("Mediator Failed,UnkNown Error: ${e.message.toString()}")
            LoadResult.Error(e)
        }
    }
}

RemoteMediator 方法(无线索)

@ExperimentalPagingApi
class ShopPageMediator(
    private val shopRepository: ShopPagingRepository,private val query: QueryHolder,private val shopDB: ShopDatabase
): RemoteMediator<QuerySnapshot,Product>() {

    private val shopDAO = shopDB.shopDao()
    
    override suspend fun load(
        loadType: LoadType,state: PagingState<QuerySnapshot,Product>,): MediatorResult {
        val loadKey = when(loadType) {
            LoadType.REFRESH -> null

            LoadType.PREPEND -> return MediatorResult.Success(endOfPaginationReached = true)

            LoadType.APPEND -> {
                val lastItem = state.lastItemOrNull() ?: return MediatorResult.Success(endOfPaginationReached = true)

                // why lastitem.id,here my lastitem is product. Does this indicate the end of the page?
                lastItem.id 
            }
        }

        val currentPage = shopRepository.getCurrentPage(
            query
        )

        shopDB.withTransaction {
            if (loadType == LoadType.PREPEND) {
            //  Todo(shopDAO.deleteByQuery(query))
            }
            shopDAO.insertAll(currentPage.toObjects())
        }

        val nextPage: QuerySnapshot? = if (currentPage.size() != 0) {
            val lastDocumentSnapShot = currentPage.documents[currentPage.size() - 1]
            shopRepository.getNextPage(lastDocumentSnapShot,query)
        } else null

        // Didn't use the result of loadkey anywhere..
        return MediatorResult.Success(
            endOfPaginationReached = nextPage == null
        )
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)