After some investigation, I realized you need to add the suspend modifier to @Update function to ensure that the updated item is emitted instantly when you update it, but I am unsure if this is the correct approach. Here is the code I'm working with:
@Dao
interface ItemDao {
@Update
suspend fun updateItem(item: Item)
}
On your way,
private var _items = MutableStateFlow<Result<List<Item>>>(Result.Loading)
val items: StateFlow<Result<List<Item>>> = _items.asStateFlow()
init {
viewModelScope.launch {
repo.getItems().collect { items ->
try {
_items.value = Response.Success(items)
} catch (e: Exception) {
_items.value = Response.Failure(e)
}
}
}
}
And inside the composable:
val itemsResponse by viewModel.items.collectAsState()
Now the updated item will be included instantly.