Android Paging 3: How To Change Parameters Of Remotemediator
I am struggling with the Paging 3 Library of Jetpack. I setup Retrofit for the network API calls Room to store the retrieved data A repository that exposes the Pager.flow (see cod
Solution 1:
The overall output of Paging is a Flow<PagingData>
, so typically mixing your signal (file path) into the flow via some flow-operation will work best. If you're able to model the path the user clicks on as a Flow<String>
, something like this might work:
ViewModel.kt
classMyViewModel extends .. {
valpathFlow= MutableStateFlow<String>("/")
valpagingDataFlow= pathFlow.flatMapLatest { path ->
Pager(
remoteMediator = MyRemoteMediator(path)
...
).flow.cachedIn(..)
}
}
RemoteMediator.kt
classMyRemoteMediatorextendsRemoteMediator<..> {
overridesuspendfunload(..): .. {
// If path changed or simply on whenever loadType == REFRESH, clear db.
}
}
The other strategy if you have everything loaded is to pass the path directly into PagingSource
, but it sounds like your data is coming from network so RemoteMediator
approach is probably best here.
Post a Comment for "Android Paging 3: How To Change Parameters Of Remotemediator"