Search code examples
androidmvvmviewmodelandroid-livedata

How to call LiveData again


I have a LiveData for fetching data from server and this LiveData may receive some time error response, so i need to fetch data again. can i notify that data resource that refresh server request again?

val movies : LiveData<Result<Movies>>? by lazy {
        model.getMovies()
}

Solution

  • You can use MutableLiveData to achieve this. For example you can do something like this:

    
    val movies: MutableLiveData<Result<Movies>>? by lazy {
        model.getMovies()
    }
    
    fun retryGetMovies() {
        val response = model.getMovies()
        movies.postValue(response)  
    }