【问题标题】:How can I convert PagingSource type in the modular app in android?如何在 android 的模块化应用程序中转换 PagingSource 类型?
【发布时间】:2022-03-09 06:31:47
【问题描述】:

我有一个模块化应用程序,它有 3 个模块 featurelocalremote

我在使用 remoteMediator 的功能模块中使用了分页。

我的 remoteMediator 使用 PagingSource 但是当我想从 Dao 获取数据时,我遇到了问题:

remoteMediator 需要 PagingSource 但本地数据库返回 PagingSource

如何将 PagingSource 转换为 PagingSource

我的道:

@Dao
interface UsersDao {

    @Query("SELECT * FROM ${Constants.USERS_TABLE}")
    fun getAllUsers(): PagingSource<Int, UserEntity>

}

我的仓库:

 override fun getUsers(): Flow<PagingData<UserModel>> {
        val pagingSourceFactory = {
            githubDatabase.usersDao().getAllUsers()
        }

        return Pager(
            config = PagingConfig(pageSize = ITEMS_PER_PAGE),
            remoteMediator = AllUsersPagingSource(
                githubApi = githubApi,
                githubDatabase = githubDatabase,
                allUsersMapper = allUsersMapper,
                allUsersRemoteLocalMapper = allUsersRemoteLocalMapper
            ), pagingSourceFactory = pagingSourceFactory
        ).flow
    }

【问题讨论】:

    标签: android pagination


    【解决方案1】:

    您是否尝试在返回数据上应用映射器?
    在存储库中使用映射器将数据从 UserEntity 映射到 UserModel
    试试这样的:
    githubDatabase.usersDao().getAllUsers().map{ mapper.mapToModel(it) }
    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      相关资源
      最近更新 更多