1

I am developing an android application project in kotlin. I have room database values with coroutines. After certain time (every 30 minutes), I want to refresh those android Room database, live data values.

Can anyone please help me on how to refresh android room database values?

Here, I have added Dao and View model codings, which I have used in my project. Hence, I want to refresh those values in every 30 minutes at Fragment.

@Dao
interface PeopleNearbyDao {

    @Query("Select * FROM people_nearby_table ORDER BY userID")
    fun getAllPeopleNearbyUsers(): Flow<List<PeopleNearByModelClass>>

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insertPeopleNearbyUsers(peopleNearByModelClass: PeopleNearByModelClass)

    @Query("DELETE FROM people_nearby_table")
    suspend fun deleteAllUsers()

    @Query("DELETE FROM people_nearby_table WHERE userID = :userId")
    suspend fun deleteUser(userId: Int)

}

View model codings:

class PeopleNearbyViewModel(private val peopleNearbyRepository: PeopleNearbyRepository) :
    ViewModel() {

    val allPeoples: LiveData<List<PeopleNearByModelClass>> =
        peopleNearbyRepository.allPeoples.asLiveData()

    /**
     * Launching a new coroutine to insert the data in a non-blocking way
     */
    fun insert(peopleNearByModelClass: PeopleNearByModelClass) = viewModelScope.launch {
        peopleNearbyRepository.insert(peopleNearByModelClass = peopleNearByModelClass)
    }

    fun delete() = viewModelScope.launch {
        Log.d("PeopleNearbyViewModel", "delete() -->")
        peopleNearbyRepository.deleteAllPeoples()
    }

    fun remove(userId: Int) = viewModelScope.launch {
        peopleNearbyRepository.delete(id = userId)
    }
}
cutiko
  • 9,887
  • 3
  • 45
  • 59
chanthini begam
  • 109
  • 1
  • 11

0 Answers0