'Android - DataSource vs Repository

You hear a lot of people talking about repositories and datasources when talking about design patterns like MVVM or MVI. I've been creating repositories for things like Retrofit and API calls, and datasources for things like Firebase or other libraries. But I'm not 100% sure I understand the difference between a datasource and a repository in the Android world.

Can someone enlighten me please?

Thanks in advance.



Solution 1:[1]

As pointed out in the previous answer, going through the guide to app architecture can help understand the relation between data sources and the repository.

Here's a useful quote from the mentioned guide to clarify the relation:

Repository modules handle data operations. They provide a clean API so that the rest of the app can retrieve this data easily. They know where to get the data from and what API calls to make when data is updated. You can consider repositories to be mediators between different data sources, such as persistent models, web services, and caches.

The things you've mentioned in your question (retrofit, firebase, etc.) are all different data sources. You can remove the repository from the equation and handle all data operations individually. But the repository module serves as an abstraction layer between different data sources and the rest of your app.

Solution 2:[2]

The repository is the API that the viewmodels use to get data. The repository has access to the datasources and decides where to get the data from. See Android's Guide to app architecture.

Android MVVM architecture.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Bakon Jarser