Data always changes dynamically and so we have to keep it updated and show the latest result to users. For this reason, we need to observe the data. LiveData is a lifecycle library class that can observe the data and react.
Let's wrap the getAllUsers() function of UserDao.kt with the LiveData:
@Query("SELECT * FROM USERS")
fun getAllUsers(): LiveData<List<Users>>
The @Query("SELECT * FROM USERS") is to get all the information from the USERS table
So here is the full code of the DAO interface:
@Dao
interface UserDAO {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun addNewUser(users: Users)
@Query("DELETE FROM USERS")
fun deleteAllUsers()
@Query("SELECT * FROM USERS")
fun getAllUsers(): LiveData<List<Users>>
}
In the MainActivity, we will see how to create an Observer of the data and override the observer's onChanged() function.