sopuse I have a function in my Room DAO like this:
@Query("SELECT * FROM cached_tbl ORDER BY id")
fun getAll(): Flowable<List<Item>>
this returns all the items in the database but I don't want this, I want the data to be paginated and be emitted in little chunks. I want the data to be loaded from the database on demand, for example, 100 items per page. is there a way to do that?
Jetpack has a library for this called Paging which you might be interested in. The good thing about using Room is that it also has Paging integration, so setup will go something like:
Dao
@Query("SELECT * FROM cached_tbl ORDER BY id")
fun getAll(): PagingSource<Int, Item>
ViewModel
val pager = Pager(PagingConfig(pageSize)) { dao.getAll() }
.cachedIn(viewModelScope)
Activity
lifecycleScope.launch {
pager.flow.collectLatest {
PagingDataAdapter.submtiData(it)
}
}
You can read quite a bit more here: https://developer.android.com/topic/libraries/architecture/paging/v3-overview , including how to setup a layered source, transformations and more.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.