Room with Flow and RecyclerView with ListAdapter

75 Views Asked by At

I have a Large Dataset in Room and it want to show it on RecyclerView but the app freezes due to huge dataset.

I am using Dao -> Repository -> ViewModel -> Activity/Fragment (To show in RecyclerView using ListAdapter)

Dao:

@Query("SELECT * FROM item_table")
fun getAllFlowItems(): Flow<List<Item>>

Repository:

fun getAllFlowItems(): Flow<List<Item>> {
    return dao.getAllFlowItems()
}

ViewModel:

fun getAllFlowItems(): Flow<List<Item>> {
    return repository.getAllFlowItems()
}

MainActivity/Fragment:

lifecycleScope.launch(Dispatchers.IO) {
    viewModel.getAllFlowItems().collectLatest() {
        adapter.submitList(it)
    }
}
0

There are 0 best solutions below