Yongho Ahn
Yongho Ahn

Reputation: 7

Local/Remote DataSource in Android MVVM with Repository pattern

I'm trying to implement the MVVM+Repository Pattern described in here and I want to cache local files and web server data into SQL DB at the same time. (My goal is ViewModel<-Repository always gets a data from SQL DB, not directly from Retrofit or Local ContentResolver)

In the current state, when ViewModel requests the repository to return data, then the Repository returns the cached data from SQL DB and if the cache is not valid then refresh from RemoteDataSource(Retrofit). I'm currently collecting local file lists using ContentResolver Cursor and created a DataSource for local files, And I successfully added local and web data separately. However, I'm confused that where should ContentResolver scanner go in this architecture. Should I treat this class as a Local DataSource? or Remote DataSource?

Repository.kt

class Repository(
    //Injected from Hilt
    val localDataSource: LocalDataSource,
    val remoteDataSource: RemoteDataSource,
    val localFileDataSource: RemoteDataSource,
    ) {  //caching code }

structure

Upvotes: 0

Views: 3772

Answers (1)

hardik9850
hardik9850

Reputation: 1086

Basics:

  • Repository's role is to provide data from different data sources mainly local and remote.
  • Content providers are part of android framework that allows us to share data between different applications.

ContentProviders can sit at the same level as Local Repository. Create a data source that will be emitting the content provider data that you are interested into and inject the object in Local repository.

Bonus: You can find a sample application here

Upvotes: 2

Related Questions