DEV Community

Cover image for Android: Repository pattern using Room, Retrofit and Coroutines
Rodrigo Silva
Rodrigo Silva

Posted on

Android: Repository pattern using Room, Retrofit and Coroutines

The repository pattern is an abstraction used to hide the multiple data sources we may have in our application, data in an application may come from an internal database, or, an external service such as a Web API.
This pattern is adopted and widely used when developing Android applications, it's also the recommended approach to creating an application.
The following diagram displays a generic mobile application architecture on Android.

Alt Text

What we can take from this diagram is the following:
Our Activity/Fragment may or may not have one, or multiple instances of different ViewModels, each view model has a dependency to a specific repository, this repository can be shared by multiple view models.
The repository knows the data sources from where to retrieve information, in this case, the repository knows the Model, which is Room, a layer on top of SQLite, and a service interface, which is provided by Retrofit in order to communicate to a web service.
Each layer only knows the layer below. The ViewModel doesn't know who the repository interacts with.
Let's give a concrete implementation example.
We will start with a sample activity, an EditText to write a reminder and a button to add the reminder.


class CreateReminderActivity : AppCompatActivity() {

    val viewModel: CreateReminderViewModel by lazy {
        val app = application as ReminderApp
        val viewModelProviderFactory =

    override fun onCreate(savedInstanceState: Bundle?) {

        val reminderEditText: EditText = findViewById(
        val createReminderButton: Button = findViewById(

        createReminderButton.setOnClickListener {
                text = reminderEditText.text.toString()

    private fun createReminder(text: String) {
        if (text.isEmpty()) {
            showToast(message = "Reminder text field is empty")
        } else {
            viewModel.createReminder(text = text)

    private fun showToast(message: String) {

Enter fullscreen mode Exit fullscreen mode


class ReminderApp : Application() {

    companion object {
        lateinit var retrofit: Retrofit
        lateinit var reminderDb: ReminderDb

    override fun onCreate() {
        reminderDb = Room

        retrofit = Retrofit.Builder()
Enter fullscreen mode Exit fullscreen mode



class CreateReminderViewModelProviderFactory(val app: ReminderApp, val intent: Intent) :
    ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {

        val reminderDao = ReminderApp.reminderDb.reminderDao()
        val reminderService = ReminderApp.retrofit.create(
        val reminderRepository = ReminderRepository(
            reminderDao = reminderDao,
            reminderService = reminderService
        val viewModel = CreateReminderViewModel(reminderRepository)
        return viewModel as T
Enter fullscreen mode Exit fullscreen mode


class CreateReminderViewModel(private val reminderRepository: ReminderRepository) : ViewModel() {

    fun createReminder(text: String) =
        viewModelScope.launch {
            reminderRepository.createReminder(text = text)
Enter fullscreen mode Exit fullscreen mode

createReminder uses viewModelScope in order to launch a coroutine.
Any coroutine launched in this scope is automatically cancelled if the ViewModel is cleared. Coroutines are useful here for when you have work that needs to be done only if the ViewModel is active. For example, if you are computing some data for a layout, you should scope the work to them ViewModel so that if the ViewModel is cleared, the work is cancelled automatically to avoid consuming resources.


class ReminderRepository(
    private val reminderService: ReminderService,
    private val reminderDao: ReminderDao
) {

    suspend fun getReminders(): LiveData<List<Programme>> = liveData {
        val reminders = reminderService.getReminders().toReminders()

    suspend fun createReminders(text: String): Reminder {
        val reminderToBeCreated = ReminderInputModelDto(text = text)
        val reminder = reminderService.createReminder(reminderToBeCreated).toReminder()
        return reminder
Enter fullscreen mode Exit fullscreen mode


interface ReminderDao {

    fun getAllReminders(): LiveData<List<Reminder>>

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun createReminder(vararg reminders: Reminder)

Enter fullscreen mode Exit fullscreen mode


interface ReminderService {

    suspend fun getReminders(): RemindersOutputModelDto

    suspend fun createReminder(@Body reminder: ReminderInputModelDto): ReminderOutputModelDto

Enter fullscreen mode Exit fullscreen mode

This article tries to give a possible implementation of the recommended approach when designing an Android application.

Top comments (0)