https://kotlinlang.org logo
#android
Title
# android
k

Khan

10/26/2019, 11:08 AM
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

rkeazor

10/26/2019, 11:59 AM
you shouldn't need to pass in a viewmodelscope. Why not just make the functions your repository suspend functions
1
k

Khan

10/26/2019, 1:35 PM
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

rkeazor

10/26/2019, 1:43 PM
where are you calling it from?
k

Khan

10/26/2019, 1:46 PM
From viewmodel i am calling PagedListDataSource class and from there i am calling Repository
r

rkeazor

10/26/2019, 1:47 PM
Do you call the actual method in the viewmodel
k

Khan

10/26/2019, 1:49 PM
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

rkeazor

10/26/2019, 1:51 PM
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

Khan

10/26/2019, 2:16 PM
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
3 Views