Open eugeneroz opened 5 years ago
Hi @eugeneroz
For that, you need to change the approach of initializing the live data in viewmodel from lazy to var or val,
var example: () -> LiveData<Result<SampleResponse>> = { repository.sample(createRequest()) }
use like this
`var liveData: LiveData<Result<List
fun refresh(): MediatorLiveData<Result<List<Product>>> {
Log.d("info_refresh","refresh2")
if(liveData!=null&&liveData?.hasObservers()!!){
liveData?.removeObservers(lifecycleOwner!!)
}
liveData = repository.products()
try{
if(mediatorLiveData.hasObservers()){
Log.d("info_refresh","removing observers")
mediatorLiveData?.removeObservers(lifecycleOwner!!)
}
}catch (exc : Exception){
Log.d("info_refresh","exception: "+exc.message)
exc.printStackTrace()
}
mediatorLiveData.addSource(liveData!!, object : Observer<Result<List<Product>>> {
override fun onChanged(@Nullable recipeListPojos: Result<List<Product>>) {
mediatorLiveData.setValue(recipeListPojos)
}
})
return mediatorLiveData
}`
in repository add this method
fun products (): LiveData<Result<List<Product>>> { Log.d("info_refresh","products refresh info") return resultLiveData( databaseQuery = { dao.getProductsLiveData() }, networkCall = { ProductRemoteDataSource.fetchProductList("",0,20) }, saveCallResult = { dao.insertAll(it.results!!) }) }
liveData?.removeObservers(lifecycleOwner!!)
@prakashreddyofferly How do you get lifecycleOwner inside viewmodel
@vignesh6 you can follow my approach, and let me know in case of any problem
Reproduce steps:
Actual: Data is presented without loading it from network
Expected: Data should be loaded from network