Search code examples
androidmvvmrepositoryandroid-contentproviderandroid-architecture-components

Repository vs Content Providers in MVVM


In Google recommended architecture https://developer.android.com/jetpack/docs/guide, they use Repository to query data from different data sources. I would be glad to better understand the following points:

  1. whether Repository is used instead of Content Providers in MVVM?

  2. if not, where would Content Providers sit in the MVVM pattern?

  3. most importantly, what are the main differences between Content Providers and Repository in this case?

Thank you for your answers enter image description here


Solution

  • 1. whether Repository is used instead of Content Providers in MVVM?

    No, repository is not replacing Content Providers, they both are different things.

    2. if not, where would Content Providers sit in the MVVM pattern?

    Create a data source class which will be handling all your communication with your content provider. This data source class can be passed to your repository.

    3. most importantly, what are the main differences between Content Providers and Repository in this case?

    Repository will be a class we create to provide data from different data sources. Content providers are part of android framework(like activity) which allows us to share data between different applications.

    enter image description here

    Here in the image you can see that we have created a data source class for communicating with content provider. Likewise we have created another data source for remote api calls. Both these data sources will directly communicate with our repository class which will act as a single source of data.

    You can find a sample implementation in this link, which shows fetching contacts