Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

308
Vistas
Which is better to put the Coroutine call in the Repository or the ViewModel?

Just wondering others opinion I have 2 ways I can go about doing something and was curious which is better (and hopefully why you think so)

I have 2 files WordRepository and WordViewModel. I can either do the coroutines in the Repo or in the ViewModel both ways work, but hoping someone can give me a clue as to why I would do the coroutines in one or the other and vice versa.

Version A.(Where the coroutine is in the Repo)

WordRepo:

class WordRepository(private val wordDao: WordDao): WordRepo {

    @WorkerThread
    override suspend fun deleteAllLogsOlderThan(XDays: Int): Int = withContext(IO) {
        return@withContext wordDao.deleteAll()
    }

}

WordViewModel:

class WordViewModel(private val wordRepository: WordRepo) : ViewModel() {

    fun deleteAllLogsOlderThanA(XDays:Int): Int = runBlocking {
        wordRepository.deleteAllLogsOlderThan(XDays)
    }

}

Version B.(Where the coroutine is in the ViewModel)

Word Repo:
class WordRepository(private val wordDao: WordDao): WordRepo {

    @WorkerThread
    override suspend fun deleteAllLogsOlderThan(XDays: Int): Int = wordDao.deleteAll()

}

WordViewModel:
class WordViewModel(private val wordRepository: WordRepo) : ViewModel() {

    fun deleteAllLogsOlderThanA(XDays:Int): Int = runBlocking {
            withContext(IO) {
                wordRepository.deleteAllLogsOlderThan(XDays)
            }
        }

}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I think either way is fine honestly but if I have to choose I prefer keeping thread related code in the repository layer. (Version A in the example)

My justification:

(A) View Models should not assume how Repositories internally work. Version B implies that the View Model assumes that the Repository will run on the calling thread.

(B) Also, the repository should not depend on other components to use a specific thread to call its method. The repository should be complete by itself.

(C) To avoid code duplication. Having multiple View models calling one repository method is a very common situation. Version A wins in this case because you only need Corountine in one place, the repository.


I suggest looking at some of the Google Samples to see where they like to handle Coroutine and data access threading codes.

  1. Sunflower uses Coroutine code in their repository. This is probably particularly useful for you because it includes examples of fire-and-forget type of requests.

  2. GitHubBrowserRepo does not use Coroutine but the repository keeps reference to the Executor instances that are used when accessing the data.

over 4 years ago · Santiago Trujillo Denunciar

0

The question is where to specify that repository jobs should run on IO threads pools:

  • A) In Repository
  • B) In ViewModel

Frankly I am not sure which way is better.

B) Having it in ViewModel means more transparency of what runs on which thread. This is also the way I mostly worked with RxJava. As a negative your ViewModel will be cluttered with thread switching (withContext()), while it's actually obvious that all repository job should run on the background thread. So is this extra information any useful?

A) Having it in Repository means more flexibility regarding thread switching in repository and cleaner code in ViewModel. Code will be less explicit from ViewModel point of view about threads. As a negative all repository methods will need to be suspended, so thus usable only from coroutines.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda