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

102
Visualizações
Coroutine scope on Application class android

I need a reference to coroutine scope on my android Application. i did the following

class TodoApplication : Application() {
    private var job = Job()
    private val applicationScope = CoroutineScope(Dispatchers.Main + job)

    val tasksRepository: TasksRepository
        get() = ServiceLocator.provideTasksRepository(this, applicationScope)

}

Is this the way to do it. If so how can I cancel coroutines launched on this scope job.cancel()

Application class don't have onDestroy method as Activities

over 4 years ago · Hanz Gallego
4 Respostas
Responde à pergunta

0

GlobalScope will be suitable for Application instance.The get() method of taskRepository variable will work as Provider Pattern. It shouldn't be in app instance. It can be replace with lazy method.

over 4 years ago · Hanz Gallego Relatório

0

NO , GlobalScope will NOT be suitable for Application instance.

As mention here in this article here:


There are multiple reasons why you shouldn’t use GlobalScope:

  • Promotes hard-coding values. It might be tempting to hardcode Dispatchers if you use GlobalScope straight-away. That’s a bad practice!

  • It makes testing very hard. As your code is going to be executed in an uncontrolled scope, you won’t be able to manage execution of work started by it.

  • You can’t have a common CoroutineContext for all coroutines built into the scope as we did with the applicationScope. Instead, you’d have to pass a common CoroutineContext to all coroutines started by GlobalScope.


So , the solution for this is to create your own scope !!

val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)

We don’t need to cancel this scope since we want it to remain active as long as the application process is alive, so we don’t hold a reference to the SupervisorJob. We can use this scope to run coroutines that need a longer lifetime than the calling scope might offer in our app.

over 4 years ago · Hanz Gallego Relatório

0

You can use onLowMemory() to release the CoroutineScope() on application class. onLowMemory() is similar to onDestroy()

val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)

clear the scope

override fun onLowMemory() {
    super.onLowMemory()
    applicationScope.cancel()
}

onLowMemory method to release any caches or other unnecessary resources you may be holding on to, The system will perform a garbage collection for you after returning from this method.

over 4 years ago · Hanz Gallego Relatório

0

Based on the existing answers, I came up with this solution:

class YourApplication : Application() {

    companion object {
        var applicationScope = MainScope()
    }
    
    override fun onLowMemory() {
        super.onLowMemory()
        applicationScope.cancel("onLowMemory() called by system")
        applicationScope = MainScope()
    }
}

The onLowMemory() part is somewhat optional, but seems a good idea, since unfinished jobs could sit there for the lifetime of your application and use system resources.

over 4 years ago · Hanz Gallego Relatório
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
Recomende algumas ofertas para mim
Preciso de ajuda