Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

305
Visualizações
Kotlin: define a coroutine scope in App class

Is it a good practice to declare a coroutine scope inside the App class instead of using a Globascope?

class App : Application() {

    val applicationJob = Job()

    companion object {
        lateinit var instance: App
            private set

        lateinit var appDefaultScope: CoroutineScope
    }

    override fun onCreate() {
        super.onCreate()
        instance = this@App
        appDefaultScope = CoroutineScope(Dispatchers.Default + applicationJob)
    }

}

Here I defined a variable appDefaultScope and initialized it in the App's onCreate

So anywhere in my code I could always do:

App.appDefaultScope.launch {
   // some operations
}

The main difference with a GlobalScope that I see is that here I can always cancel the job and shut down all potentially stuck coroutines.

Are there are better alternatives to this?

The reason is that i have some object functions that use coroutines and are not called from an activity (then I cannot provide them with a scope defined inside the activity) but need a scope for their operations. An example of such object function is a Log that writes on a local database that can be called almost everywhere in the App.

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Assistente virtual