I am investigating the new Android Room Paging library
implementation "androidx.paging:paging-runtime-ktx:3.0.0-alpha09"
My source database tables approx 10,000 rows and I filter by the first character of the name field as follows:-
DAO
@Query("SELECT * from citation_style WHERE citation_style_name LIKE :startsWith ORDER BY citation_style_name ASC")
fun fetch(startsWith: String): PagingSource<Int, CitationStyleDO>
Repository
fun fetch(startsWith: String): Flow<PagingData<CitationStyleDO>> {
return Pager(
PagingConfig(pageSize = 60, prefetchDistance = 30, enablePlaceholders = false, maxSize = 200)
) { database.citationStyleDao().fetch("$startsWith%") }.flow
}
ViewModel
fun fetch(startsWith: String): Flow<PagingData<CitationStyleDO>> {
return repository.fetch(startsWith).cachedIn(viewModelScope)
}
Fragment
override fun onStartsWithClicked(startsWith: String) {
lifecycleScope.launch {
viewModel.fetch(startsWith).collectLatest { adapter.submitData(it) }
}
}
Is this the correct approach of repeatedly using lifecycleScope.launch {...} each time the Starts With character is changed?
Should I be map{} or switchMap{} triggered by a MutabaleLiveData<String> for StartwWith?