我正在研究新的 Android Room Paging 库
implementation "androidx.paging:paging-runtime-ktx:3.0.0-alpha09"
我的源数据库表大约有 10,000 行,我按名称字段的第一个字符进行过滤,如下所示:-
道
@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) }
}
}
这是lifecycleScope.launch {...}
每次更改 Starts With 字符时重复使用的正确方法吗?
我应该被map{}
或switchMap{}
由MutabaleLiveData<String>
for StartwWith 触发吗?