Я преобразовал ответ сервера в MutableLiveData , как показано ниже в коде:
private fun fetchFromNetwork(dbSource: LiveData<T>) {
result.addSource(dbSource) { newData -> result.setValue(Resource.loading(newData)) }
createCall().enqueue(object : Callback<V> {
override fun onResponse(call: Call<V>, response: Response<V>) {
result.removeSource(dbSource)
// here converting server response in to MutableLiveData
val converted: MutableLiveData<T> = MutableLiveData()
converted.value = response.body() as T
result.addSource(converted) { newData ->
if (null != newData)
result.value = Resource.success(newData)
}
}
override fun onFailure(call: Call<V>, t: Throwable) {
result.removeSource(dbSource)
result.addSource(dbSource) { newData ->
result.setValue(
Resource.error(
getCustomErrorMessage(t),
newData
)
)
}
}
})
}