我正在调查新的Android Room分页库
implementation "androidx.paging:paging-runtime-ktx:3.0.0-alpha09"
我的源数据库表大约有10,000行,我按照名称字段的首个字符进行筛选,如下所示:
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>
代码库
fun fetch(startsWith: String): Flow<PagingData<CitationStyleDO>> {
return Pager(
PagingConfig(pageSize = 60, prefetchDistance = 30, enablePlaceholders = false, maxSize = 200)
) { database.citationStyleDao().fetch("$startsWith%") }.flow
}
视图模型
fun fetch(startsWith: String): Flow<PagingData<CitationStyleDO>> {
return repository.fetch(startsWith).cachedIn(viewModelScope)
}
碎片
override fun onStartsWithClicked(startsWith: String) {
lifecycleScope.launch {
viewModel.fetch(startsWith).collectLatest { adapter.submitData(it) }
}
}
每次更改Starts With字符时重复使用 lifecycleScope.launch {...}
是正确的方法吗?
对于StartsWith,我应该使用由 MutabaleLiveData<String>
触发的 map{}
或 switchMap{}
?