Hi guys! I have a question regarding paging compon...
# android
k
Hi guys! I have a question regarding paging component. My structure is something like this: ViewModel -> Datasource -> Repository. I am passing viewModelScope to Datasource and starting coroutine inside DataSource class, which is then calling Repository where actual api call is being made. Its working fine for me. But i wanna know if there is anything wrong with this implementation structure.
Repository is fetching data from both sources. But its not being called directly from viewmodel but from datasource class.
Something like this
r
you shouldn't need to pass in a viewmodelscope. Why not just make the functions your repository suspend functions
1
k
My repository functions are suspend. But thing is i am not calling repo from viewmodel and if i don't pass viewModelScope to DataSource then i can't call repository suspend function.
r
where are you calling it from?
k
From viewmodel i am calling PagedListDataSource class and from there i am calling Repository
r
Do you call the actual method in the viewmodel
k
I created Datasourcefactory in view model which is observing livedata for livePagedList. In datasource i am calling Repository method which is making api call on the basis on page number
r
now I am offically lost😅. How can a viewmodel be observing livedata when its not a Lifecycle owner? do you have the link to the github
k
Ah 😄 i am sorry for writing incorrectly. Fragment is observing livedata.
This is my viewmodel
This is snippet of Repository.
My code is working completely fine though. i wanna know is if having a mediator like datasource between viewmodel & repository is fine.
DataSource is supposed to be invalidated when the backing data changes, so having it fetch data from an abstraction that hides whether it came from db or network might make it harder to know when to invalidate
and DataSource.Factory is supposed to give you a new DataSource when you invalidate
in the layered case for network + db our example simply writes into db and invalidates whenever we fetch new data from network