2

I implement paging 3 follow this codelab. codelab But when i create UserPagingRepository, i have problem that data class is not map. In Room DataBase, i use UserLocal otherhand i use UserRemote for data get from api. How can I convert data from data layer to domain layer (class 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) ) } } 

fix:

pager.flow // Type is Flow<PagingData<User>>. .map { pagingData -> pagingData.map { user -> UiModel(user) } } 
1
  • You should accept the answer, it was useful to find the solution. Commented Oct 26, 2022 at 22:09

1 Answer 1

5

In flow downstream like in ViewModel, when you call for paging data apply map before collecting.

searchRepo.searchUser(it) .map { user -> UserData.asDomainLayer() } .cachedIn(viewModelScope) 
Sign up to request clarification or add additional context in comments.

1 Comment

oh , oke i will call map twice

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.