I'm getting some data from my database like this
Pager(PagingConfig(pageSize = PAGE_SIZE)) {
songRepo.getSongs()
}.flow.cachedIn(viewModelScope)
and I collect it in the UI using collectAsLazyPagingItems(), but whenever I navigate to another screen and I go back the data is clearing.
Is there a solution to this or I should create my own pagination without Paging?
Update
This is the best result I could achieve so far
val state by viewModel.state.collectAsStateWithLifecycle()
val songs = viewModel.songs.collectAsLazyPagingItems()
LaunchedEffect(songs.itemSnapshotList.items.size) {
if (songs.itemCount > 0) {
viewModel.setSongs(songs)
}
}
state.songs?.let {
items(state.songs, key = { item -> item.song.songId!! }) { item ->}
}
view model:
private val _state = MutableStateFlow<LazyPagingItems<SongInfo>?>( null)
val state = _state.asStateFlow()
fun setSongs(d: LazyPagingItems<SongInfo>) {
_state.update {
it.copy(songs = d)
}
}
val songs = Pager(PagingConfig(pageSize = PAGE_SIZE)) {
songRepo.getSongs()
}.flow.cachedIn(viewModelScope)