我按照下面的代码实现分页3。codelab但是当我创建UserPagingRepository时,我有数据类不是映射的问题。在房间数据库中,我使用UserLocal,另一方面,我使用UserRemote从api获取数据。如何将数据从数据层转换到域层(类User)。
const val NETWORK_PAGE_SIZE = 20
class UserPagingRepository(
private val service: UserApiService,
private val database: UserDatabase
) {
fun getUsersPaging(): Flow<PagingData<User>> {
val pagingSourceFactory = { database.usersDao().getPagingUsers() }
return Pager(
config = PagingConfig(
pageSize = NETWORK_PAGE_SIZE,
enablePlaceholders = false
),
pagingSourceFactory = pagingSourceFactory,
remoteMediator = UserRemoteMediator(service, database)
)
}
}
修复:
pager.flow // Type is Flow<PagingData<User>>.
.map { pagingData ->
pagingData.map { user -> UiModel(user) }
}
在像ViewModel一样的下游流中,当你调用分页数据时,在收集之前应用map。
searchRepo.searchUser(it)
.map { user ->
UserData.asDomainLayer()
}
.cachedIn(viewModelScope)