79165850

Date: 2024-11-07 09:58:43
Score: 0.5
Natty:
Report link

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.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Update
  • Low reputation (0.5):
Posted by: Santhosh Kumar