I use Kotlin Flow and LiveData to fetch objects from room database. I want to know that the value of the object in the database has changed. I did the following;
UserDao
@Query("SELECT * FROM user WHERE id=:userId")
fun getUserById(userId: String?) : Flow<User>
UserRepository
fun getUserById(userId: String?) : Flow<User> = userDao.getUserById(userId);
ViewModel
val userId = MutableStateFlow<String>("")
val user : LiveData<User> = userId.flatMapLatest { id ->
userRepository.getUserById(id)
}.asLiveData()
There is no problem while fetching the data but when I change the user's name, the "user.name" value is not updated.
Note: Changing in database, checked.
I update the name as follows:
@Query("UPDATE user SET name = :name WHERE id = :userId")
suspend fun updateName(name: String, userId: String)