I'm trying to get data from server and cache into database and return new fetched list to user. I'm getting response form server and saving it to the local database but when im trying to observer it from composable function it showing list is empty.
When i try to debug and collect flow data in myViewModel class it showing but it not showing is composable function.
dao
@Dao
interface CategoryDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(categories: List<Category>)
@Query("SELECT * FROM categories ORDER BY name")
fun read(): Flow<List<Category>>
@Query("DELETE FROM categories")
suspend fun clearAll()
}
repository class:
suspend fun getCategories(): Flow<List<Category>> {
val categories = RetrofitModule.getCategories().categories
dao.insert(categories)
return dao.read()
}
myViewModel
fun categoriesList(): Flow<List<Category>> {
var list: Flow<List<Category>> = MutableStateFlow(emptyList())
viewModelScope.launch {
list = repository.getCategories().flowOn(Dispatchers.IO)
}
return list
}
Observing from:
@Composable
fun StoreScreen(navController: NavController, viewModel: CategoryViewModel) {
val list = viewModel.categoriesList().collectAsState(emptyList())
Log.d("appDebug", list.value.toString()) // Showing always emptyList []
}
current response :
2021-05-15 16:08:56.017 5125-5125/com.demo.app D/appDebug: []
> = MutableStateFlow(emptyList())` @Jonas
– Santanu Sur May 15 '21 at 11:23